Unity3d-C#之语法特性

来源:互联网 发布:讲题数学软件 编辑:程序博客网 时间:2024/05/16 09:45

?? 运算符:http://msdn.microsoft.com/zh-cn/library/ms173224(v=VS.100).aspx

?? 运算符称为 null 合并运算符,用于定义可以为 null 值的类型和引用类型的默认值。 如果此运算符的左操作数不为 null,则此运算符将返回左操作数;否则返回右操作数。

下面代码源于Unity3d 4.6UI系统源码:UI / UnityEngine.UI / EventSystem / Raycasters / PhysicsRaycaster.cs

 public override Camera eventCamera        {            get            {                if (m_EventCamera == null)                    m_EventCamera = GetComponent<Camera>();                return m_EventCamera ?? Camera.main;            }        }

ref 关键字:http://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx

ref 关键字通过引用(而非值)传递参数。 通过引用传递的效果是,对所调用方法中的参数进行的任何更改都反映在调用方法中

下面代码源于:UI / UnityEngine.UI / UI / Core / SetPropertyUtility.cs

        public static bool SetClass<T>(ref T currentValue, T newValue) where T : class        {            if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))                return false;            currentValue = newValue;            return true;        }

out 关键字通过引用传递参数:http://msdn.microsoft.com/zh-cn/library/t3c3bfhx.aspx

下面代码源于:UI / UnityEngine.UI / EventSystem / InputModules / PointerInputModule.cs
     protected bool GetPointerData(int id, out PointerEventData data, bool create)        {            if (!m_PointerData.TryGetValue(id, out data) && create)            {                data = new PointerEventData(eventSystem)                {                    pointerId = id,                };                m_PointerData.Add(id, data);                return true;            }            return false;        }
隐式类型 var:http://msdn.microsoft.com/zh-cn/library/bb383973.aspx
下面代码源于:UI / UnityEngine.UI / EventSystem / InputModules / PointerInputModule.cs
   protected static PointerEventData.FramePressState StateForMouseButton(int buttonId)        {            var pressed = Input.GetMouseButtonDown(buttonId);            var released = Input.GetMouseButtonUp(buttonId);            if (pressed && released)                return PointerEventData.FramePressState.PressedAndReleased;            if (pressed)                return PointerEventData.FramePressState.Pressed;            if (released)                return PointerEventData.FramePressState.Released;            return PointerEventData.FramePressState.NotChanged;        }



0 0