-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.cs
129 lines (105 loc) · 2.98 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using Random = UnityEngine.Random;
public static class Extensions
{
#region STRUCTS
public static byte[] ToByteArray<T>(this T structure) where T : struct
{
var bufferSize = Marshal.SizeOf(structure);
var byteArray = new byte[bufferSize];
IntPtr handle = Marshal.AllocHGlobal(bufferSize);
try
{
Marshal.StructureToPtr(structure, handle, true);
Marshal.Copy(handle, byteArray, 0, bufferSize);
}
finally
{
Marshal.FreeHGlobal(handle);
}
return byteArray;
}
public static T BuildFrom<T>(this T structure, byte[] bytes) where T : struct
{
var bufferSize = Marshal.SizeOf(typeof(T));
var handle = Marshal.AllocHGlobal(bufferSize);
Marshal.Copy(bytes, 0, handle, bufferSize);
var returnVal = Marshal.PtrToStructure<T>(handle);
Marshal.FreeHGlobal(handle);
return returnVal;
}
#endregion
#region TRANSFORM
public static Vector3 TransformPointUnscaled(this Transform transform, Vector3 position)
{
var localToWorldMatrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
return localToWorldMatrix.MultiplyPoint3x4(position);
}
public static Vector3 InverseTransformPointUnscaled(this Transform transform, Vector3 position)
{
var worldToLocalMatrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one).inverse;
return worldToLocalMatrix.MultiplyPoint3x4(position);
}
public static Transform Reset(this Transform transform)
{
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
return transform;
}
public static void ZeroAll(this Transform t)
{
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
}
public static void ZeroPosRot(this Transform t)
{
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
}
#endregion
#region GETCREATE
public static T GetCreateComponent<T>(this MonoBehaviour c) where T : UnityEngine.Component
{
return c.gameObject.GetCreateComponent<T>();
}
public static T GetCreateComponent<T>(this GameObject go) where T : UnityEngine.Component
{
T comp = go.GetComponent<T>();
if (comp == null)
comp = go.AddComponent<T>();
return comp;
}
public static T GetCreateComponent<T>(this Transform t) where T : UnityEngine.Component
{
return t.gameObject.GetCreateComponent<T>();
}
public static T GetCreateComponent<T>(this Component t) where T : UnityEngine.Component
{
return t.gameObject.GetCreateComponent<T>();
}
#endregion
#region RANDOM
public static T GetRandom<T>(this IList<T> list)
{
if (list == null || list.Count <= 0)
{
return default(T);
}
return list[Random.Range(0, list.Count)];
}
public static T GetRandom<T>(this T[] list)
{
if (list == null || list.Length <= 0)
{
return default(T);
}
return list[Random.Range(0, list.Length)];
}
#endregion
}