Unity C# 学习笔记

GeometryUtility

Unsafe | Native

  • 开启Unsafe
    • Project Setting/Player
    • .asmdef
      • Unity.Burst
      • Unity.Mathematics
  • 获取native指针
    • NativeArray.GetUnsafePtr()
    • NativeArray.GetUnsafeReadOnlyPtr()
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      using Unity.Collections.LowLevel.Unsafe;

      struct SpheresSoA
      {
      [ReadOnly] public NativeArray<float> centerX;

      public unsafe int HitSpheres(ref Ray r, float tMin, float tMax, ref Hit outHit)
      {
      float4* ptrCenterX = (float4*) centerX.GetUnsafeReadOnlyPtr();
      float4 sCenterX = *ptrCenterX;
      }
      }

SerializedObject | ScriptObject | Reflection

  • Type
    • typeof(class)
    • object.GetType()
  • Asset Load
    • const string assetPath = “Assets/Settings/Universal Render Pipeline Asset.asset”;
    • var pipelineAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, typeof(RenderPipelineAsset)) as RenderPipelineAsset;
  • SerializedObject
    • 支持Undo
    • 支持多个编辑
    • Property
      • FindProperty
      • SerializedProperty
        • Next
        • NextVisible
        • intValue
      • you can Shift+Right Click on property names in the Inspector to see their paths
      • Shift+右键获取属性路径
    • ApplyModifiedProperties
    • 序列化对象 SerializedObject
  • ScriptableObject

Command Buffer

Unity技巧

  • 方法属性加Button
  • Unity小窍门100条!!!(上)
  • FormerlySerializedAs()
    • 重命名显示
  • RuntimeInitializeOnLoadMethod
    • [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
      • 无需创建空物体就能执行代码
      • 在运行游戏(Play)就会立即执行该函数,而不需要继承MonoBehavior并挂接在物体身上。

Extension

1
2
3
4
5
6
// 拓展Vector3方法,关键使用this
public static Vector3 Round(this Vector3 v)
{
xxxx
return v;
}

Mono

  • OnValidate() 属性变化时调用

UnityEvent

  • 支持UI事件函数编辑
  • using UnityEngine.Events;
  • public UnityEvent onClick;

Custom Package

  • package.json
    • name “com.xxx.core”
    • displayName “xxx”
    • version “0.0.1”
  • 解决csproj生成问题,使用最新Vscode Editor Package1.2.5插件
  • Asmdef
    • 模块化打包成dll

Build Player

C# Native Command

1
2
3
4
5
6
7
8
9
10
var cmd_info = new ProcessStartInfo
{
FileName = "notepad", //cmd
Arguments = "", //args
// Verb = "runas", //管理员运行
// WorkingDirectory = ".", //cwd
};
Process process = Process.Start(cmd_info);
process.WaitForExit();
process.Close();

DLL


Unity C# 学习笔记
https://automask.github.io/wild/2021/10/26/log/P_Unity_Csharp/
作者
Kyle Zhou
发布于
2021年10月26日
许可协议