Unity一些很有用的技巧

一: 性能:多线程射线检测

在使用中经常需要每帧进行射线,性能消耗比较大,那么可以使用Unity.Collections.RaycastCommand进行多线程的检查,将耗能操作放到子线程去.同样其他的形状检测.Unity官方文档:

RaycastCommand

struct in UnityEngine

描述

用于设置要在作业过程中异步执行的射线投射命令的结构。

使用此结构计划一批射线投射时,它们会以异步方式并行执行。射线投射的结果会写入结果缓冲区。由于结果是异步写入,因此在作业完成之前无法访问结果缓冲区。

命令缓冲区中索引 N 处的命令的结果会存储在结果缓冲区中索引 N * maxHits 处。

如果 maxHits 大于命令的实际结果数,则结果缓冲区会包含一些未命中任何对象的无效结果。第一个无效结果通过为 null 的碰撞体进行标识。第二个以及随后的无效结果不会由射线投射命令写入,因此其碰撞体不保证为 null。遍历结果时,循环应在发现第一个无效结果时停止。

示例:

胶囊体落地就会变红,因为检测到了地面,性能可以查看性能监视器,发现所有操作移到了子work中去了 

using UnityEngine;
using Unity.Collections;
using Unity.Jobs;

/// <summary>
/// 多线程射线检测测试
/// </summary>
public class RaycastCommandTest : MonoBehaviour
{

    Material myMaterial;
    NativeArray<RaycastCommand> raycastRaycastCommandNativeArray;
    NativeArray<RaycastHit> raycastHitNativeArray;
    JobHandle jobHandle;

    void Start()
    {
        myMaterial = this.GetComponent<MeshRenderer>().material;
        raycastRaycastCommandNativeArray = new NativeArray<RaycastCommand>(1, Allocator.Persistent);
        raycastHitNativeArray = new NativeArray<RaycastHit>(10, Allocator.Persistent);
    }

    // Update is called once per frame
    void Update()
    {

        //this.OldPhysicsTest();
        this.RaycastCommandTestFuc();

    }
    /// <summary>
    /// 异步的检测方式
    /// </summary>
    private void RaycastCommandTestFuc()
    {
        //---完成上一帧的job
        this.jobHandle.Complete();
        //---检查结果
        for (int i = 0; i < raycastHitNativeArray.Length; i++)
        {
            if (raycastHitNativeArray[i].collider != null && raycastHitNativeArray[i].collider.tag == "Ground")
            {
                myMaterial.SetColor("_BaseColor", Color.red);
                break;
            }
            else
            {
                myMaterial.SetColor("_BaseColor", Color.white);
            }
        }

        //----设置这一帧的射线检测参数


        raycastRaycastCommandNativeArray[0] = new RaycastCommand(this.transform.position, Vector3.down, 1.1f, -5, 10);

        this.jobHandle = RaycastCommand.ScheduleBatch(raycastRaycastCommandNativeArray, raycastHitNativeArray, 1);

    }
    /// <summary>
    /// 旧的使用方式
    /// </summary>
    private void OldPhysicsTest()
    {
        RaycastHit hitInfo = new RaycastHit();


        if (Physics.Raycast(this.transform.position, Vector3.down, out hitInfo, 1.1f) && hitInfo.collider.tag == "Ground")
        {
            myMaterial.SetColor("_BaseColor", Color.red);

        }
        else
        {
            myMaterial.SetColor("_BaseColor", Color.white);
        }
    }


    void OnDestroy()
    {
        this.jobHandle.Complete();
        this.raycastRaycastCommandNativeArray.Dispose();
        this.raycastHitNativeArray.Dispose();
    }
}

 二:批量规则放置物体

当需要有规则或者随机放置多个物体的时候,可以一起选择他们,然后再position里面输入函数来放置他们,如果现有将他们排队放置 那么在X里面输入 L(0,12),代表将这13个物体每单位放一个,R(-5,5)即在-5到5的范围内随机放置他们

三:独立的物体属性窗口,提高工作效率

选择物体,然后点击右键-点击properties,可以开启这个物体的独立属性窗口,当你选择其他物体的时候,这个窗口依然可以对原物体有效,相当于inspector窗口锁定某个物体的状态,那么我们就不再需要在Inspector窗口锁定物体才能拖入拖出赋值了,当然也可以将某个物体的某个组件独立打开properties窗口,只需要在组件上点右键选择properties即可

 

四:c#新的语法糖,将默认属性的值作为序列化对象

猜你喜欢

转载自blog.csdn.net/qq_33347809/article/details/125702102