Unity一些很有用的技巧和实现

采用倒序是为了将新添加的内容放到前面,方便随时查阅 

五:检查鼠标是否点击在ui上

场景中要进行鼠标点击3d物体时,需要首先屏蔽掉玩家点击按钮的这一次点击。所以需要判断鼠标是不是点击在ui上(会自动忽略未启用raycast功能的ui)

    List<RaycastResult> results = new List<RaycastResult>();

    private bool BeClickOnUI()
    {
        var pointEvent = new PointerEventData(EventSystem.current);
        pointEvent.position = Input.mousePosition;
        results.Clear();
        EventSystem.current.RaycastAll(pointEvent, results);
        //注意要获得正确的结果,则需要保证鼠标未点击在任何开启了raycast功能的UI上,没有开启RayCast的会自动忽略
        return results.Count > 0;
    }

 

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

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

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

 

 二:批量规则放置物体

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

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

在使用中经常需要每帧进行射线,性能消耗比较大,那么可以使用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();
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值