Unity学习笔记:NGUI常用脚本

场景初始化

using UnityEngine;
using System.Collections;

public class UIInit : MonoBehaviour {

	private UIManager mUIManager;

	private void Start()
	{
		Object obj = FindObjectOfType (typeof(UIManager));
		if (obj)
			mUIManager = obj as UIManager;
		if (mUIManager == null) {
			GameObject uiManager = new GameObject ("UIManager");
			mUIManager = uiManager.AddComponent ();
		}
		mUIManager.InitializeUIs ();
		mUIManager.ShowUI ();
	}


}

查找子物体

using UnityEngine;
using System.Collections;

public abstract class Global
{
	public static string LoadUIName = "";
	public static string LoadSceneName = "";
	public static bool isBattle = false;

	public static bool Contain3DScene = false;

	public static string PlayerName = "";

	public static GameObject FindChild(Transform trans , string childName)
	{
		Transform child = trans.FindChild(childName);
		if (child != null)
		{
			return child.gameObject;
		}
		int count = trans.childCount;
		GameObject go = null;
		for(int i = 0 ; i < count ; ++i)
		{
			child = trans.GetChild(i);
			go = FindChild(child, childName);
			if (go != null)
				return go;
		}
		return null;
	}
	//查找子物体 并且返回它的组件
	public static T FindChild(Transform trans, string childName) where T : Component
	{
		GameObject go = FindChild(trans,childName);
		if(go == null)
			return null;
		return go.GetComponent();
	}
	/// 
	/// 获取时间格式字符串,显示mm:ss
	/// 
	/// The minute time.
	/// Time.
	public static string GetMinuteTime(float time)
	{
		int mm,ss;
		string stime = "0:00";
		if (time<=0) return stime;
		mm = (int)time/60;
		ss = (int)time%60;
		if(mm>60)
			stime = "59:59";
		else if (mm <10 && ss >=10)
		{
			stime = "0" + mm + ":" + ss;
		}else if (mm<10&&ss<10)
		{
			stime = "0"+mm+":0"+ss;
		}else if (mm>=10&&ss<10)
		{
			stime = mm+":0"+ss;
		}
		else
		{
			stime= mm+":"+ss;
		}
		return stime;
	}
}

UI管理器

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DevelopEngine;

public class UIName
{
	public const string UIRole = "UIScene_Role";
	public const string UIRoleInfo = "UIScene_RoleInfo";
	public const string UIBackpack = "UIScene_Backpack";
	public const string UIBackpackInfo = "UIScene_BackpackInfo";
}
public class UIManager : MonoSingleton {

	private Dictionary mUIScene = new Dictionary();
	private Dictionary mUIAnchor = new Dictionary();

	public void InitializeUIs()
	{
		mUIAnchor.Clear();
		Object[] objs = FindObjectsOfType(typeof(UIAnchor));
		if (objs != null)
		{
			foreach(Object obj in objs)
			{
				UIAnchor uiAnchor = obj as UIAnchor;
				if (!mUIAnchor.ContainsKey(uiAnchor.side))
					mUIAnchor.Add(uiAnchor.side, uiAnchor.gameObject);
			}
		}
		mUIScene.Clear();
		Object[] uis = FindObjectsOfType(typeof(UIScene));
		if (uis != null)
		{
			foreach (Object obj in uis)
			{
				UIScene ui = obj as UIScene;
				ui.SetVisible(false);
				mUIScene.Add(ui.gameObject.name, ui);
			}
		}
	}

	public void SetVisible (string name, bool visible)
	{
		if (visible && !IsVisible(name))
		{
			OpenScene(name);
		}
		else if (!visible && IsVisible(name))
		{
			CloseScene(name);
		}
	}

	public bool IsVisible (string name)
	{
		UIScene ui = GetUI(name);
		if (ui != null)
			return ui.IsVisible();		
		return false;
	}
	private UIScene GetUI(string name)
	{
		UIScene ui;
		return mUIScene.TryGetValue(name , out ui) ? ui : null;
	}

	public T GetUI (string name) where T : UIScene
	{
		return GetUI(name) as T;
	}

	private bool isLoaded(string name)
	{
		if (mUIScene.ContainsKey(name))
		{
			return true;
		}
		return false;
	}

	private void OpenScene(string name)
	{
		if (isLoaded(name))
		{
			mUIScene[name].SetVisible(true);
		}
	}
	private void CloseScene(string name)
	{
		if (isLoaded(name))
		{
			mUIScene[name].SetVisible(false);
		}
	}

	/// 	/// 显示一级界面	/// 
	public void ShowUI()
	{
		SetVisible (UIName.UIRole,true);
		SetVisible (UIName.UIBackpack, true);
	}
}

UI层管理器

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ESceneType
{
	Main = 0,
	Nomal = 1,
	Popup = 2,
	Announce = 3,
}
public class UIScene : MonoBehaviour {

	protected string mUIName = "";

	private Dictionary mUIWidgets = new Dictionary();

	public UIAnchor.Side side = UIAnchor.Side.Center;

	public ESceneType type = ESceneType.Nomal;

	protected virtual void Start()
	{
		this.FindChildWidgets(gameObject.transform);
	}

	protected virtual void Update()
	{

	}

	public virtual bool IsVisible()
	{
		return gameObject.activeSelf;
	}
	public virtual void SetVisible(bool visible)
	{
		gameObject.SetActive(visible);
	}
	protected UISceneWidget GetWidget (string name)
	{
		// If allready find out, return 
		if (mUIWidgets.ContainsKey(name))
			return mUIWidgets[name];
		
		// Find out widget with name and add to dictionary
		Transform t = gameObject.transform.FindChild(name);
		if (t == null) return null;
		
		UISceneWidget widget = t.gameObject.GetComponent();
		if (widget != null)
		{
			mUIWidgets.Add(widget.gameObject.name, widget);
		}
		
		return t.gameObject.GetComponent();
	}
	protected T GetWidget (string name) where T : Component
	{
		// Find out widget with name and add to dictionary
		GameObject go = GameObject.Find(name);
		if (go == null) return null;
		
		T widget = go.GetComponent();
		
		return widget;
	}
	private void FindChildWidgets(Transform t)
	{
		UISceneWidget widget = t.gameObject.GetComponent();
		if (widget != null)
		{
//			Debug.LogWarning("FindChildWidgets Parent[" + t.name + "] " + t.gameObject.name);
			string name = t.gameObject.name;
			if (!mUIWidgets.ContainsKey(name))
			{
				mUIWidgets.Add(name , widget);
			}
			else
			{
//				Debug.LogWarning("Scene[" + this.transform.name + "]UISceneWidget[" + name + "]is exist!");
			}
		}
		for(int i = 0; i < t.childCount ; ++i)
		{
			Transform child = t.GetChild(i);
			FindChildWidgets(child);
		}
	}

}

UI按钮行为类

using UnityEngine;
using System.Collections;
using System;

/// 
/// 界面控件基类
/// 
public class UISceneWidget : MonoBehaviour 
{
	DateTime OnClickTime;
	public float Throughtime = 0.5f;
	/// - OnHover (isOver) 悬停,悬停时传入true,移出时传入false
	public delegate void onMouseHover (UISceneWidget eventObj, bool isOver);
	public onMouseHover OnMouseHover = null;
	void OnHover (bool isOver)
	{
		if (OnMouseHover != null) OnMouseHover(this, isOver);
	}
	/// - OnPress (isDown)按下时传入true,抬起时传入false
	public delegate void onMousePress (UISceneWidget eventObj, bool isDown);
	public onMousePress OnMousePress = null;
	void OnPress (bool isDown)
	{
		if (OnMousePress != null) OnMousePress(this, isDown); 
	}
	/// - OnSelect 相似单击,区别在于选中一次以后再选中将不再触发OnSelect
	public delegate void onMouseSelect (UISceneWidget eventObj, bool selected);
	public onMouseSelect OnMouseSelect = null;
	void OnSelect (bool selected)
	{
		if (OnMouseSelect != null) OnMouseSelect(this, selected); 
	}
	/// - OnClick 单击 Throughtime点击间隔时间
	public delegate void onMouseClick (UISceneWidget eventObj);
	public onMouseClick OnMouseClick = null;
	void OnClick ()
	{
		if (Throughtime > (float)(DateTime.UtcNow - OnClickTime).TotalSeconds)
		{
			return;
		}
		OnClickTime = DateTime.UtcNow;

		if (OnMouseClick != null)	OnMouseClick(this);
	}
	/// - OnDoubleClick 双击(双击间隔小于0.25秒)时触发。
	public delegate void onMouseDoubleClick(UISceneWidget eventObj);
	public onMouseDoubleClick OnMouseDoubleClick = null;
	void OnDoubleClick ()
	{
		if (OnMouseDoubleClick != null) OnMouseDoubleClick(this);
	}
	/// - OnDrag 按下并移动时触发,delta为传入的位移
	public delegate void onMouseDrag (UISceneWidget eventObj, Vector2 delta);
	public onMouseDrag OnMouseDrag = null;
	void OnDrag (Vector2 delta)
	{
		if (OnMouseDrag != null) OnMouseDrag(this, delta); 
	}
	public delegate void onMouseDrop (UISceneWidget eventObj, GameObject dropObject);
	public onMouseDrop OnMouseDrop = null;
	void OnDrop (GameObject dropObject)
	{
		if (OnMouseDrop != null) OnMouseDrop(this, dropObject); 
	}
	/// - OnInput (text) is sent when typing (after selecting a collider by clicking on it).
	/// - OnTooltip (show) is sent when the mouse hovers over a collider for some time without moving.
	/// - OnScroll (float delta) is sent out when the mouse scroll wheel is moved.
	/// - OnKey (KeyCode key) is sent when keyboard or controller input is used.
}

 

你可能感兴趣的:(c#,unity3d)