“图像”不包含“纹理”的定义,并且没有可访问的扩展方法

问题描述 投票:0回答:1

“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;
        }
    }
}
c# unity-game-engine 2d
1个回答
0
投票

假设该项目是来自 fx pack 的“CFX_Demo_GTToggle”脚本

碰撞矩形= this.GetComponent().GetPixelAdjustedRect();

您需要的答案:

this.GetComponent().sprite = Sprite.Create((Texture2D)Hover, 新矩形(0, 0, 悬停.宽度, 悬停.高度), 0.5f * Vector2.one);

© www.soinside.com 2019 - 2024. All rights reserved.