Unity 高亮组件使用

429 阅读1分钟

LiangAction 类


[RequireComponent(typeof(HighlightableObject))]
public class LiangAction : HighLightControl
{
    //public HighlightableObject obj;
    private void Start()
    {
        // m_ho.FlashingOn(Color.blue,Color.red,1);
        m_ho = GetComponent<HighlightableObject>();
        m_ho.ConstantOn(Color.blue);
    }

    private void OnDestroy()
    {
        Debug.LogError("我被销毁了");
        Destroy(GetComponent<HighlightableObject>());
    }
}

配合页面摄像机挂载的脚本中 代码块

			GameObject.Find("Cube").AddComponent<LiangAction>();//添加组件
		}
		if (Input.GetKeyDown(KeyCode.Q))
		{
		 Destroy(GameObject.Find("Cube").GetComponent<LiangAction>());
		}

实现效果: 1.点击Cube添加高亮组件 2.字母Q 移除高亮组件 3.显示:cube变蓝 鼠标指上自动变色

[RequireComponent(typeof(....))]

当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体)  用法:在新建的类前面加 [RequireComponent(typeof(HighlightableObject))] ![](https://p 1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/46df7f69c0fe4eeaa31397afa5b954db~tplv-k3u1fbpfcp-zoom-1.image)