GUI实现,如下:
按“G”键开始新手引导
代码如下:
using UnityEngine; using System.Collections; public class OkButton : GUIBase { /// <summary> /// 按钮图片 /// </summary> public Texture2D OKBTN_a; public Texture2D OKBTN_b; /// <summary> /// 引导图片 /// </summary> public Texture2D HoleTexture; private GUIStyle OKBTNStyle; private GUIStyle _defaultStyle; // Use this for initialization public override void Start () { base.Start(); OKBTNStyle = new GUIStyle(); _defaultStyle = new GUIStyle(); _defaultStyle.padding = new RectOffset(0, 0, 0, 0); _defaultStyle.border = new RectOffset(0, 0, 0, 0); if (OKBTN_a == null) { OKBTN_a = Resources.Load("OKBTN/OK_a") as Texture2D; OKBTNStyle.normal.background = OKBTN_a; } if (OKBTN_b == null) { OKBTN_b = Resources.Load("OKBTN/OK_b") as Texture2D; OKBTNStyle.active.background = OKBTN_b; } if (HoleTexture == null) HoleTexture = Resources.Load("OKBTN/okfight") as Texture2D; } // Update is called once per frame public override void Update () { base.Update(); } private bool _isGuider = false; void LateUpdate() { if (Input.GetKeyDown(KeyCode.G)) { _isGuider = true; } } public override void OnGUI() { base.OnGUI(); if (_isGuider) //引导 { GUI.DrawTexture(new Rect(0, 0, 800, 480), HoleTexture); Rect rect = new Rect(674, 355, 128, 128); OkButton.AddHole(rect, _defaultStyle); if (Event.current.type == EventType.MouseUp) { if (rect.Contains(Event.current.mousePosition)) { StartCoroutine(disCatch()); } } } if (GUI.Button(new Rect(674, 355, 128, 128), "", OKBTNStyle)) { print("Click OKBTN"); _isGuider = false; } if (GUI.Button(new Rect(0, 0, 128, 128), "", OKBTNStyle)) { print("Click Other"); } } /// <summary> /// 另外线程处理 /// </summary> /// <returns></returns> IEnumerator disCatch() { //开一个线程处理要处理的事 yield return 1; } /// <summary> /// 在界面上面开孔,只有此处可以点击,其余的地方屏蔽用户点击,即在该按钮四周加其他按钮,先加的按钮会在上层。后加的按钮被屏蔽。 /// </summary> /// <param name="rect"></param> public static void AddHole(Rect rect, GUIStyle style) { GUI.Button(new Rect(0, 0, GUIBase.DesignStageWidth, rect.y), "", style);//上面的button GUI.Button(new Rect(0, rect.yMax, GUIBase.DesignStageWidth, GUIBase.DesignStageHeight - rect.yMax), "", style); //下边的button GUI.Button(new Rect(0, rect.y, rect.x, rect.height), "", style);//左边 GUI.Button(new Rect(rect.xMax, rect.y, GUIBase.DesignStageWidth - rect.xMax, rect.height), "", style);//右边 } }