Forest_Client/Forest/Assets/PhxhSDK/Phxh/Utils/Utils.UI.cs

65 lines
1.9 KiB
C#
Raw Normal View History

2024-06-12 15:01:54 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace PhxhSDK
{
public static partial class Utils
{
public static void SetImage(GameObject go, string name)
{
var img = go?.GetComponent<Image>();
SetImage(img, name);
}
public static void SetImage(GameObject go, Sprite icon)
{
var img = go?.GetComponent<Image>();
if (img != null) img.sprite = icon;
}
public static void SetImage(Image img, string name)
{
if (img != null)
{
if (string.IsNullOrEmpty(name))
{
img.sprite = null;
}
// var asset = ResourcesManager.Instance.LoadResource<Sprite>(name);
// if (asset != null) {
// SetImage(img, asset);
// }
}
}
// public static void SetAtlasImage(Image img, string atlasName, string name) {
// if (img != null) {
// if (string.IsNullOrEmpty(atlasName) || string.IsNullOrEmpty(name)) {
// img.sprite = null;
// } else {
// var asset = ResourcesManager.Instance.GetSpriteVariant(atlasName, name);
// if (asset != null) {
// SetImage(img, asset);
// }
// }
// }
// }
public static void SetImage(Image img, Sprite sprite)
{
if (img != null) img.sprite = sprite;
}
public static Vector3 WorldPos2ScreenPos(Vector3 worldPos, Camera camera)
{
var screenPos = camera.WorldToScreenPoint(worldPos);
return screenPos;
}
public static Vector3 ScreenPos2WorldPos(Vector3 screenPos, Camera camera)
{
var worldPos = camera.ScreenToWorldPoint(screenPos);
return worldPos;
}
}
}