unity 开发中10个小知识(一)

现在记忆力越来越差,写过很多遍的内容,都有可能需要慢慢才能想起来,这里就记录下在unity开发过程中一些小的知识点

一、获取unity层级和layerMask

 int ground = LayerMask.NameToLayer("Ground");

 int groundMask = 1<<ground;


二、获取鼠标拾取位置

    public static  class ULayerMask
    {
        public static int Ground = LayerMask.NameToLayer("Ground");
        public static int GroundMask = 1<<LayerMask.NameToLayer("Ground");

        public static int Wall = LayerMask.NameToLayer("Wall");

        public static int WallMask = 1 << Wall;

        public static int CollisionMask = 1<< Ground | 1<< Wall;



    }

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit,1000f,ULayerMask.CollisionMask)) 
            {
                ULog.Error("astar");
                foreach (var actor in charactors.GetActors())
                {
                    actor.Astar?.BeginPath(hit.point);
                }
            }


 三、获取触摸输入的位置

for (var i = 0; i < Input.touchCount; ++i)
{
    if (Input.GetTouch(i).phase == TouchPhase.Began)
    {
        // Construct a ray from the current touch coordinates
        Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
        // Create a particle if hit
        if (Physics.Raycast(ray))
        {
            //ULog.Info(ray.ToString());
        }
    }
} 

四、FariyGui 判断鼠标等输入设备是否在UI之上

 Stage.isTouchOnUI


五、fairyGui动态创建UIPanel

GameObject uiObject = new GameObject();
uiObject.layer = ULayerMask.UI;


UIPanel uiPanel = uiObject.AddComponent<UIPanel>();

uiPanel.packageName = package;
uiPanel.componentName = componentName;

uiPanel.CreateUI();


 六、fairyGui为控件添加Touch事件

  GObject _touchArea;

_touchArea.onTouchBegin.Add(this.OnTouchBegin);
 _touchArea.onTouchMove.Add(this.OnTouchMove);
_touchArea.onTouchEnd.Add(this.OnTouchEnd);

七 C#中??的使用

a??b;

当a不为空时返回a,否则返回b

八、C#中 ?. 的使用

a?.name

如果a为空返回空,否则返回a.name

string name = a?.name;

九、如何对泛型进行约束

约束为T必须继承BaseComponent,并且带默认构造函数

public void RemoveComponent<T>() where  T : BaseComponent,new()
{
    RemoveComponent(typeof(T));

}


十、绕y轴选旋转一定角度

public void RotationY(float angle)
{

    transform.rotation = Quaternion.Euler(Vector3.up * (angle % 360));
}

猜你喜欢

转载自blog.csdn.net/lejian/article/details/131074848