“Image”不包含“texture”的定义,并且找不到接受“Image”类型的第一个参数的可访问扩展方法“texture”(您是否缺少 using 指令或程序集引用?
using UnityEngine;
using UnityEngine.UI;
public class CFX_Demo_GTToggle : MonoBehaviour
{
public Texture Normal;
public Texture Hover;
public Color NormalColor = new Color32(128, 128, 128, 128);
public Color DisabledColor = new Color32(128, 128, 128, 48);
public bool State = true;
public string Callback;
public GameObject Receiver;
private Rect CollisionRect;
private bool Over;
private Image Label;
private void Awake()
{
CollisionRect = GetComponent<Rect>().GetScreenRect(Camera.main);
Label = GetComponentInChildren<Text>();
UpdateTexture();
}
private void Update()
{
if (CollisionRect.Contains(Input.mousePosition))
{
Over = true;
if (Input.GetMouseButtonDown(0))
{
OnClick();
}
}
else
{
Over = false;
GetComponent< Image>().color = NormalColor;
}
UpdateTexture();
}
private void OnClick()
{
State = !State;
Receiver.SendMessage(Callback);
}
private void UpdateTexture()
{
Color color = (!State) ? DisabledColor : NormalColor;
if (Over)
{
GetComponent<Image>().texture = Hover;
}
else
{
GetComponent<Image>().texture = Normal;
}
GetComponent<Image>().color = color;
if (Label != null)
{
Label.color = color * 1.75f;
}
}
}
假设该项目是来自 fx pack 的“CFX_Demo_GTToggle”脚本
碰撞矩形= this.GetComponent
().GetPixelAdjustedRect();
您需要的答案:
this.GetComponent().sprite = Sprite.Create((Texture2D)Hover, 新矩形(0, 0, 悬停.宽度, 悬停.高度), 0.5f * Vector2.one);