diff options
author | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-05-08 23:15:13 +0800 |
commit | d07e14add74e017b52ab2371efeea1aa4ea10ced (patch) | |
tree | efd07869326e4c428f5bfe43fad0c2583d32a401 /Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility |
+init
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility')
10 files changed, 416 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs new file mode 100644 index 0000000..1a7cc5e --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace UnityEngine.UI +{ + internal static class ListPool<T> + { + // Object pool to avoid allocations. + private static readonly ObjectPool<List<T>> s_ListPool = new ObjectPool<List<T>>(null, l => l.Clear()); + + public static List<T> Get() + { + return s_ListPool.Get(); + } + + public static void Release(List<T> toRelease) + { + s_ListPool.Release(toRelease); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs.meta new file mode 100644 index 0000000..e54f9f7 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ListPool.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 12520643c37454344b5a1dd60cc2bdbd +timeCreated: 1602119377 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs new file mode 100644 index 0000000..b506d80 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using UnityEngine.Events; + +namespace UnityEngine.UI +{ + internal class ObjectPool<T> where T : new() + { + private readonly Stack<T> m_Stack = new Stack<T>(); + private readonly UnityAction<T> m_ActionOnGet; + private readonly UnityAction<T> m_ActionOnRelease; + + public int countAll { get; private set; } + public int countActive { get { return countAll - countInactive; } } + public int countInactive { get { return m_Stack.Count; } } + + public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease) + { + m_ActionOnGet = actionOnGet; + m_ActionOnRelease = actionOnRelease; + } + + public T Get() + { + T element; + if (m_Stack.Count == 0) + { + element = new T(); + countAll++; + } + else + { + element = m_Stack.Pop(); + } + if (m_ActionOnGet != null) + m_ActionOnGet(element); + return element; + } + + public void Release(T element) + { + if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element)) + Debug.LogError("Internal error. Trying to destroy object that is already released to pool."); + if (m_ActionOnRelease != null) + m_ActionOnRelease(element); + m_Stack.Push(element); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs.meta new file mode 100644 index 0000000..942cd48 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ObjectPool.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: e74077e6b05099a4589f683a80cc92ff +timeCreated: 1602119380 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs new file mode 100644 index 0000000..638fa03 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace UnityEngine.UI +{ + internal class ReflectionMethodsCache + { + public delegate bool Raycast3DCallback(Ray r, out RaycastHit hit, float f, int i); + public delegate RaycastHit2D Raycast2DCallback(Vector2 p1, Vector2 p2, float f, int i); + public delegate RaycastHit[] RaycastAllCallback(Ray r, float f, int i); + public delegate RaycastHit2D[] GetRayIntersectionAllCallback(Ray r, float f, int i); + + // We call Physics.Raycast and Physics2D.Raycast through reflection to avoid creating a hard dependency from + // this class to the Physics/Physics2D modules, which would otherwise make it impossible to make content with UI + // without force-including both modules. + public ReflectionMethodsCache() + { + var raycast3DMethodInfo = typeof(Physics).GetMethod("Raycast", new[] {typeof(Ray), typeof(RaycastHit).MakeByRefType(), typeof(float), typeof(int)}); + if (raycast3DMethodInfo != null) + raycast3D = (Raycast3DCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(Raycast3DCallback), raycast3DMethodInfo); + + var raycast2DMethodInfo = typeof(Physics2D).GetMethod("Raycast", new[] {typeof(Vector2), typeof(Vector2), typeof(float), typeof(int)}); + if (raycast2DMethodInfo != null) + raycast2D = (Raycast2DCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(Raycast2DCallback), raycast2DMethodInfo); + + var raycastAllMethodInfo = typeof(Physics).GetMethod("RaycastAll", new[] {typeof(Ray), typeof(float), typeof(int)}); + if (raycastAllMethodInfo != null) + raycast3DAll = (RaycastAllCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(RaycastAllCallback), raycastAllMethodInfo); + + var getRayIntersectionAllMethodInfo = typeof(Physics2D).GetMethod("GetRayIntersectionAll", new[] {typeof(Ray), typeof(float), typeof(int)}); + if (getRayIntersectionAllMethodInfo != null) + getRayIntersectionAll = (GetRayIntersectionAllCallback)UnityEngineInternal.ScriptingUtils.CreateDelegate(typeof(GetRayIntersectionAllCallback), getRayIntersectionAllMethodInfo); + } + + public Raycast3DCallback raycast3D = null; + public RaycastAllCallback raycast3DAll = null; + public Raycast2DCallback raycast2D = null; + public GetRayIntersectionAllCallback getRayIntersectionAll = null; + + private static ReflectionMethodsCache s_ReflectionMethodsCache = null; + + public static ReflectionMethodsCache Singleton + { + get + { + if (s_ReflectionMethodsCache == null) + s_ReflectionMethodsCache = new ReflectionMethodsCache(); + return s_ReflectionMethodsCache; + } + } + }; +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs.meta new file mode 100644 index 0000000..8aa76ec --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/ReflectionMethodsCache.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: a4c9f67a29a841b479c2abbec238e83c +timeCreated: 1602119379 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs new file mode 100644 index 0000000..15d8519 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using UnityEngine.Events; + +namespace UnityEngine.UI +{ + internal static class SetPropertyUtility + { + public static bool SetColor(ref Color currentValue, Color newValue) + { + if (currentValue.r == newValue.r && currentValue.g == newValue.g && currentValue.b == newValue.b && currentValue.a == newValue.a) + return false; + + currentValue = newValue; + return true; + } + + public static bool SetStruct<T>(ref T currentValue, T newValue) where T : struct + { + if (EqualityComparer<T>.Default.Equals(currentValue, newValue)) + return false; + + currentValue = newValue; + return true; + } + + public static bool SetClass<T>(ref T currentValue, T newValue) where T : class + { + if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue))) + return false; + + currentValue = newValue; + return true; + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs.meta new file mode 100644 index 0000000..ee3936e --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/SetPropertyUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7eb311d761ee3d409de0593312d6fa2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs new file mode 100644 index 0000000..3020d4d --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; + +namespace UnityEngine.UI +{ + public class VertexHelper : IDisposable + { + //c ¸Ä³Épublic·½±ã¿´ + /*private*/ public List<Vector3> m_Positions = ListPool<Vector3>.Get(); + /*private*/ public List<Color32> m_Colors = ListPool<Color32>.Get(); + /*private*/ public List<Vector2> m_Uv0S = ListPool<Vector2>.Get(); + /*private*/ public List<Vector2> m_Uv1S = ListPool<Vector2>.Get(); + /*private*/ public List<Vector2> m_Uv2S = ListPool<Vector2>.Get(); + /*private*/ public List<Vector2> m_Uv3S = ListPool<Vector2>.Get(); + /*private*/ public List<Vector3> m_Normals = ListPool<Vector3>.Get(); + /*private*/ public List<Vector4> m_Tangents = ListPool<Vector4>.Get(); + /*private*/ public List<int> m_Indices = ListPool<int>.Get(); + + private static readonly Vector4 s_DefaultTangent = new Vector4(1.0f, 0.0f, 0.0f, -1.0f); + private static readonly Vector3 s_DefaultNormal = Vector3.back; + + public VertexHelper() + {} + + public VertexHelper(Mesh m) + { + m_Positions.AddRange(m.vertices); + m_Colors.AddRange(m.colors32); + m_Uv0S.AddRange(m.uv); + m_Uv1S.AddRange(m.uv2); + m_Uv2S.AddRange(m.uv3); + m_Uv3S.AddRange(m.uv4); + m_Normals.AddRange(m.normals); + m_Tangents.AddRange(m.tangents); + m_Indices.AddRange(m.GetIndices(0)); + } + + public void Clear() + { + m_Positions.Clear(); + m_Colors.Clear(); + m_Uv0S.Clear(); + m_Uv1S.Clear(); + m_Uv2S.Clear(); + m_Uv3S.Clear(); + m_Normals.Clear(); + m_Tangents.Clear(); + m_Indices.Clear(); + } + + public int currentVertCount + { + get { return m_Positions.Count; } + } + public int currentIndexCount + { + get { return m_Indices.Count; } + } + + public void PopulateUIVertex(ref UIVertex vertex, int i) + { + vertex.position = m_Positions[i]; + vertex.color = m_Colors[i]; + vertex.uv0 = m_Uv0S[i]; + vertex.uv1 = m_Uv1S[i]; + vertex.uv2 = m_Uv2S[i]; + vertex.uv3 = m_Uv3S[i]; + vertex.normal = m_Normals[i]; + vertex.tangent = m_Tangents[i]; + } + + public void SetUIVertex(UIVertex vertex, int i) + { + m_Positions[i] = vertex.position; + m_Colors[i] = vertex.color; + m_Uv0S[i] = vertex.uv0; + m_Uv1S[i] = vertex.uv1; + m_Uv2S[i] = vertex.uv2; + m_Uv3S[i] = vertex.uv3; + m_Normals[i] = vertex.normal; + m_Tangents[i] = vertex.tangent; + } + + public void FillMesh(Mesh mesh) + { + mesh.Clear(); + + if (m_Positions.Count >= 65000) + throw new ArgumentException("Mesh can not have more than 65000 vertices"); + + mesh.SetVertices(m_Positions); + mesh.SetColors(m_Colors); + mesh.SetUVs(0, m_Uv0S); + mesh.SetUVs(1, m_Uv1S); + mesh.SetUVs(2, m_Uv2S); + mesh.SetUVs(3, m_Uv3S); + mesh.SetNormals(m_Normals); + mesh.SetTangents(m_Tangents); + mesh.SetTriangles(m_Indices, 0); + mesh.RecalculateBounds(); + } + + public void Dispose() + { + ListPool<Vector3>.Release(m_Positions); + ListPool<Color32>.Release(m_Colors); + ListPool<Vector2>.Release(m_Uv0S); + ListPool<Vector2>.Release(m_Uv1S); + ListPool<Vector2>.Release(m_Uv2S); + ListPool<Vector2>.Release(m_Uv3S); + ListPool<Vector3>.Release(m_Normals); + ListPool<Vector4>.Release(m_Tangents); + ListPool<int>.Release(m_Indices); + + m_Positions = null; + m_Colors = null; + m_Uv0S = null; + m_Uv1S = null; + m_Uv2S = null; + m_Uv3S = null; + m_Normals = null; + m_Tangents = null; + m_Indices = null; + } + + public void AddVert(Vector3 position, Color32 color, Vector2 uv0, Vector2 uv1, Vector3 normal, Vector4 tangent) + { + m_Positions.Add(position); + m_Colors.Add(color); + m_Uv0S.Add(uv0); + m_Uv1S.Add(uv1); + m_Uv2S.Add(Vector2.zero); + m_Uv3S.Add(Vector2.zero); + m_Normals.Add(normal); + m_Tangents.Add(tangent); + } + + public void AddVert(Vector3 position, Color32 color, Vector2 uv0) + { + AddVert(position, color, uv0, Vector2.zero, s_DefaultNormal, s_DefaultTangent); + } + + public void AddVert(UIVertex v) + { + AddVert(v.position, v.color, v.uv0, v.uv1, v.normal, v.tangent); + } + + public void AddTriangle(int idx0, int idx1, int idx2) + { + m_Indices.Add(idx0); + m_Indices.Add(idx1); + m_Indices.Add(idx2); + } + + public void AddUIVertexQuad(UIVertex[] verts) + { + int startIndex = currentVertCount; + + for (int i = 0; i < 4; i++) + AddVert(verts[i].position, verts[i].color, verts[i].uv0, verts[i].uv1, verts[i].normal, verts[i].tangent); + + AddTriangle(startIndex, startIndex + 1, startIndex + 2); + AddTriangle(startIndex + 2, startIndex + 3, startIndex); + } + + public void AddUIVertexStream(List<UIVertex> verts, List<int> indices) + { + if (verts != null) + { + CanvasRenderer.AddUIVertexStream(verts, m_Positions, m_Colors, m_Uv0S, m_Uv1S, m_Uv2S, m_Uv3S, m_Normals, m_Tangents); + } + + if (indices != null) + { + m_Indices.AddRange(indices); + } + } + + public void AddUIVertexTriangleStream(List<UIVertex> verts) + { + if (verts == null) + return; + CanvasRenderer.SplitUIVertexStreams(verts, m_Positions, m_Colors, m_Uv0S, m_Uv1S, m_Uv2S, m_Uv3S, m_Normals, m_Tangents, m_Indices); + } + + public void GetUIVertexStream(List<UIVertex> stream) + { + if (stream == null) + return; + + CanvasRenderer.CreateUIVertexStream(stream, m_Positions, m_Colors, m_Uv0S, m_Uv1S, m_Uv2S, m_Uv3S, m_Normals, m_Tangents, m_Indices); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs.meta new file mode 100644 index 0000000..df0d085 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Utility/VertexHelper.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 462296566c1ff9443bbf18a80a326dda +timeCreated: 1602119378 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |