Digitwin技术分享_20211015

Digitwin技术分享_20211015
一、代码优化
1.闭包
Eg 1

  Button button;
    int value = 3;
    button.onClick.AddListener(() =>
    {
        int y = 2;
        int result = value + y;
    });

Eg 2

List< Button> buttons=new List<Button>();
 for (int i = 0; i < buttons.Count; i++)
        {
            buttons[i].onClick.AddListener(() =>
            {
                Debug.Log(buttons[i].name);
            });
        }

2.字典与枚举

将枚举作为字典的键值存储

public enum TimeOfDay
    {
        Moning=0,
        Afternoon=1,
        Evening=2,
    }
public class TimeOfDayComparer : IEqualityComparer<TimeOfDay>
    {
        public bool Equals(TimeOfDay x, TimeOfDay y)
        {
            return (int)x==(int)y;
        }

        public int GetHashCode(TimeOfDay obj)
        {
            return (int)obj;
        }
    }

TimeOfDayComparer comparer = new TimeOfDayComparer();
Dictionary<TimeOfDay, int> dicData = new Dictionary<TimeOfDay, int>(comparer);
 dicData.Add(TimeOfDay.Moning, 1);
        dicData.Add(TimeOfDay.Afternoon, 2);
        dicData.Add(TimeOfDay.Evening, 3);

减少因装箱/拆箱引起的GC

Answer to Eg 2

for (int i = 0; i < buttons.Count; i++)
        {
            var index = i;
            buttons[index].onClick.AddListener(() =>
            {
                Debug.Log(buttons[index].name);
            });
        }

3.变量缓存

二、UI相关
1.UI遮罩相关
a.通过EventTrigger

public static bool isPointerOverUI;

        var trigger = GetComponent<EventTrigger>();
        if (trigger == null)
            trigger = gameObject.AddComponent<EventTrigger>();
        trigger.triggers = new List<EventTrigger.Entry>();
        EventTrigger.Entry enterEntry = new EventTrigger.Entry();
        enterEntry.eventID = EventTriggerType.PointerEnter;
        enterEntry.callback = new EventTrigger.TriggerEvent();
        UnityAction<BaseEventData> enterCallback = new UnityAction<BaseEventData>((data) =>
        {
            isPointerOverUI = true;
        });
        enterEntry.callback.AddListener(enterCallback);

为遮罩可能需添加的TriggerType有PointerEnter,PointerExit,PointerDown,PointerUp,PointerClick

b.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)

EventSystem.current.IsPointerOverGameObject()
c.GraphicRaycaster.Raycast
public class UIRaycaster : MonoBehaviour
{
    List<RaycastResult> raycastResults = new List<RaycastResult>();

    void Update()
    {
        if (Input.touchCount > 0)
        {
            GraphicRaycaster(Input.GetTouch(0).position);
        }
    }
    public EventSystem eventSystem;
    public GraphicRaycaster graphicRaycaster;
    public List<RaycastResult> GraphicRaycaster(Vector2 p_Positon)
    {
        var pointer = new PointerEventData(eventSystem);
        pointer.position = p_Positon;
        raycastResults.Clear();
        graphicRaycaster.Raycast(pointer, raycastResults);
        return raycastResults;
    }
}

2.UI适配与优化
a.UI元素切分按九宫格 size=4x (尺寸影响压缩方式)
b.Canvas整体调整
PC 1920
1080 ->IOS 23881668
Canvas Scaler=1.55
左右两侧将有部分内容被裁剪
(1920
1.55-2388)/2=294

PC全屏Image有预留
1920*1334
Canvas Scaler 1.25
c.UI元素靠边停靠

3.UI优化

a.通过子Canvas或者多个Canvas去减少UI重建

三、渲染优化
1.☆确定渲染瓶颈

CPU?GPU
Profiler相关分析工具

渲染流程
-CPU计算出什么东西需要被绘制,并且怎么被绘制。
-CPU给GPU发送指令。
-GPU根据CPU的指令进行绘制。

2.优化
->降低渲染对象数量
合并mesh
修改相机远端裁剪平面

->降低渲染次数
阴影->烘焙光照贴图

->显存带宽
压缩纹理
Mipmap 多级渐远纹理

你可能感兴趣的:(Unity,3D与编程语言,unity)