diff --git a/README.md b/README.md index c2317e0..e258dc6 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ #### 版本 +* new + 添加编辑器阶段快捷生成、移除脚本 + 添加基本几何体拼接算法 * 0.2.1 修改:事件扩展 InvokeConservative 从ExtendType类中移到了MacroEvent diff --git a/Runtime/Generation.meta b/Runtime/Generation.meta new file mode 100644 index 0000000..a559820 --- /dev/null +++ b/Runtime/Generation.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 076c2a8219fca8649bdb0bfae5a3aac5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Actuator.meta b/Runtime/Generation/Actuator.meta new file mode 100644 index 0000000..b1aad7b --- /dev/null +++ b/Runtime/Generation/Actuator.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5691b4e940f93e74cbac35f477e10af7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Actuator/BaseGenerate.cs b/Runtime/Generation/Actuator/BaseGenerate.cs new file mode 100644 index 0000000..2cce490 --- /dev/null +++ b/Runtime/Generation/Actuator/BaseGenerate.cs @@ -0,0 +1,219 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using UnityEditor; + +using UnityEngine; + +using XericLibrary.Runtime.MacroLibrary; +using XericLibrary.Runtime.Type; + +using static UnityEngine.GraphicsBuffer; + +namespace XericLibrary.Runtime.Generation +{ + public class BaseGenerate : WeaklyMonoBase + where T : WeaklyObject + { + #region ֶ + + /// + /// + /// + [SerializeField] + private T GenerationCompute; + + #endregion + + #region + + + + #endregion + + #region ݾ̬ + + /// + /// һһ + /// + /// ֻ MonoBehaviours ͨ C# ֶκԣ + /// Unity TransformRigidbody ȣĸƣ + /// ΪЩֶκͨ˽еġ + /// + /// + /// + /// + /// + /// + public static COMPONENT CopyComponentToAnother(COMPONENT source, COMPONENT copied) + where COMPONENT : Component + { + if(source == null || copied == null) + { + Debug.LogWarning("Source or target component is null. Cannot copy parameters."); + return null; + } + + System.Type type = typeof(T); + var fields = type.GetFields(); + var properties = type.GetProperties(); + + // ֻǹֶ + foreach(var field in fields) + { + if(!field.IsStatic) + { + field.SetValue(copied, field.GetValue(source)); + } + } + + foreach(var property in properties) + { + if(property.CanWrite) + { + property.SetValue(copied, property.GetValue(source, null), null); + } + } + + return copied; + } + + /// + /// ӸϿ¡һµ + /// + /// ͵ + /// + /// + /// + /// + /// + /// + public static COMPONENT CopyComponentToNew(GameObject source, out GameObject copied) + where COMPONENT : Component + { + Component[] allComponents = source.GetComponents(); + copied = Instantiate(source); + + foreach(Component component in copied.GetComponents()) + { + if(component.GetType() != typeof(COMPONENT)) + { +#if UNITY_EDITOR + if(PlayModeState != PlayModeStateChange.EnteredPlayMode) + DestroyImmediate(component); + else + Destroy(component); +#else + Destroy(component); +#endif + } + } + return copied.GetComponent(); + } + + #endregion + + #region ݷ + + /// + /// ڵ + /// + protected GameObject CreatOrGetGameNode(string name, Transform parent = null) + { + var targetParent = parent == null ? transform : parent; + + var node = targetParent.GetChildren() + .Where(a => a.name == name) + .Select(a => a.gameObject) + .FirstOrDefault(); + + if(node != null) + return node; + + node = new GameObject(name); + node.transform.parent = targetParent; + return node; + } + + /// + /// ʹϷڵӾ + /// + protected void DieSonless(Transform transform) + { + foreach(var target in transform.GetChildren().ToList()) + { +#if UNITY_EDITOR + if(PlayModeState != PlayModeStateChange.EnteredPlayMode) + DestroyImmediate(target.gameObject); + else + Destroy(target.gameObject); +#else + Destroy(target.gameObject); +#endif + } + } + + /// + /// ʹϷڵӾ + /// + protected void DieSonless(GameObject target) + { + DieSonless(target.transform); + } + + /// + /// ȡȻͬһڵжָṹĸ + /// + /// + /// ҪȡĽڵ + /// ʱڵ + /// + protected void ExtractAllComponent(Transform transform, string name, int MaxStackDepth = 20) + where COMPONENT : Component + { + var root = CreatOrGetGameNode(name, transform); + DieSonless(root); + + int index = 0; + // ȡ + foreach(var comp in transform.GetComponentsInRecursionChildren(MaxStackDepth)) + { + CreatOrGetGameNode($"component.{index++}", root.transform); + CopyComponentToNew(comp.gameObject, out var copiedObj); + copiedObj.transform.parent = root.transform; + } + } + + #endregion + + } + +#if UNITY_EDITOR + + /// + /// Ĵinspecterϴť + /// + public class BaseGenerateEditor : Editor + { + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + if(GUILayout.Button("ɸ")) + UpdateGen(); + } + + /// + /// + /// + protected void UpdateGen() + { + var script = (StairsGenerate)target; + } + + + } + +#endif +} diff --git a/Runtime/Generation/Actuator/BaseGenerate.cs.meta b/Runtime/Generation/Actuator/BaseGenerate.cs.meta new file mode 100644 index 0000000..e8f1224 --- /dev/null +++ b/Runtime/Generation/Actuator/BaseGenerate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d1b51df505c013648929cef9785bce3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Actuator/GenMethod.cs b/Runtime/Generation/Actuator/GenMethod.cs new file mode 100644 index 0000000..0e71908 --- /dev/null +++ b/Runtime/Generation/Actuator/GenMethod.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEditor; + +using UnityEngine; + +using XericLibrary.Runtime.MacroLibrary; +using XericLibrary.Runtime.Type; + +namespace XericLibrary.Runtime.Generation +{ + /// + /// ɽڵ㴿 + /// + public class GenMethod : BaseGenerate + { + public string NewNodeName = "GEN_NODE_METHOD"; + } + +#if UNITY_EDITOR + + [CustomEditor(typeof(GenMethod))] + public class GenMethodEditor : BaseGenerateEditor + { + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + var script = (GenMethod)target; + + // Inspectorʾť + if(GUILayout.Button($"ײ嵽{script.NewNodeName}")) + GenMethod.CopyComponentToNew(script.gameObject, out var outobj); + + if(GUILayout.Button("Ⱦ")) + DestroyAllMesh(script.transform); + } + + /// + /// ģ + /// + public void DestroyAllMesh(Transform transform) + { + MeshFilter fl = null; + MeshRenderer mr; + + Action destroycomponent = comp => + { +#if UNITY_EDITOR + if(fl != null) + DestroyImmediate(fl); +#else + if(fl != null) + Destroy(fl); +#endif + }; + + foreach(var item in transform.GetChildrenRecursion(10)) + { + Debug.Log(item.name); + fl = item.GetComponent(); + destroycomponent(fl); + mr = item.GetComponent(); + destroycomponent(mr); + } + } + + } + +#endif + +} diff --git a/Runtime/Generation/Actuator/GenMethod.cs.meta b/Runtime/Generation/Actuator/GenMethod.cs.meta new file mode 100644 index 0000000..2623f0f --- /dev/null +++ b/Runtime/Generation/Actuator/GenMethod.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d47c476cb54b0384f9a5ed853a81c637 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Actuator/StairsGenerate.cs b/Runtime/Generation/Actuator/StairsGenerate.cs new file mode 100644 index 0000000..1b1a0ea --- /dev/null +++ b/Runtime/Generation/Actuator/StairsGenerate.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEditor; + +using UnityEngine; + +using XericLibrary.Runtime.Type; + +using static UnityEngine.GraphicsBuffer; + +namespace XericLibrary.Runtime.Generation +{ + /// + /// ¥ + /// + public class StairsGenerate : BaseGenerate + { + + } + + + +#if UNITY_EDITOR + + [CustomEditor(typeof(StairsGenerate))] + public class ButtonUIGuideEditor : BaseGenerateEditor + { + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + // Inspectorʾť + if(GUILayout.Button("̬")) + { + + } + } + + } + +#endif + +} \ No newline at end of file diff --git a/Runtime/Generation/Actuator/StairsGenerate.cs.meta b/Runtime/Generation/Actuator/StairsGenerate.cs.meta new file mode 100644 index 0000000..c522631 --- /dev/null +++ b/Runtime/Generation/Actuator/StairsGenerate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0f074c0e944db2143840f96a053a42e9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Splice.meta b/Runtime/Generation/Splice.meta new file mode 100644 index 0000000..81ec184 --- /dev/null +++ b/Runtime/Generation/Splice.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 652abfb64baca2b489235a2124321cde +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs b/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs new file mode 100644 index 0000000..0709a58 --- /dev/null +++ b/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +using XericLibrary.Runtime.Type; + +namespace XericLibrary.Runtime.Generation +{ + /// + /// ƽڿײ + /// + [Serializable] + public class PlaneWithHoleGeneration : WeaklyObject + { + + } +} \ No newline at end of file diff --git a/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs.meta b/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs.meta new file mode 100644 index 0000000..6dee5fb --- /dev/null +++ b/Runtime/Generation/Splice/PlaneWithHoleGeneration.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8024ed59544f0b346b46ff0e0d86457a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs b/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs new file mode 100644 index 0000000..8e7c07d --- /dev/null +++ b/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +using XericLibrary.Runtime.Type; + +namespace XericLibrary.Runtime.Generation +{ + /// + /// ΰΧ봩ף׵ķ + /// + [Serializable] + public class RectangularSurroundWithHoleGeneration : WeaklyObject + { + + } +} \ No newline at end of file diff --git a/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs.meta b/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs.meta new file mode 100644 index 0000000..388a06d --- /dev/null +++ b/Runtime/Generation/Splice/RectangularSurroundWithHoleGeneration.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0973eb189e2933541a254fc92cbb3476 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Generation/Splice/StairsGeneration.cs b/Runtime/Generation/Splice/StairsGeneration.cs new file mode 100644 index 0000000..6ffcabe --- /dev/null +++ b/Runtime/Generation/Splice/StairsGeneration.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +using XericLibrary.Runtime.Type; + +namespace XericLibrary.Runtime.Generation +{ + /// + /// ¥ݼײ + /// ɿѭĵ¥ݣ̶׵¥ + /// + [Serializable] + public class StairsGeneration : WeaklyObject + { + #region ֶ + + //public List<> + + #endregion + + #region ̨ײ + + //public struct StairsParameter + //{ + + //} + + #endregion + } +} \ No newline at end of file diff --git a/Runtime/Generation/Splice/StairsGeneration.cs.meta b/Runtime/Generation/Splice/StairsGeneration.cs.meta new file mode 100644 index 0000000..1169c97 --- /dev/null +++ b/Runtime/Generation/Splice/StairsGeneration.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 50cfea13c93ae974dbd744c96da5900d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/MicroLibrary/MacroList.cs b/Runtime/MicroLibrary/MacroList.cs index 223b9ab..35ad8be 100644 --- a/Runtime/MicroLibrary/MacroList.cs +++ b/Runtime/MicroLibrary/MacroList.cs @@ -14,7 +14,8 @@ namespace XericLibrary.Runtime.MacroLibrary #region ϴ㷨 /// - /// Fisher-Yatesϴ㷨ʱ临ӶΪO(n*n),ռ临ӶΪO(n) + /// Fisher-Yatesϴ㷨ʱ临ӶΪO(n*n),ռ临ӶΪO(n); + /// ȱԪ0ܴʻ /// /// ѡĿ /// Ŀ @@ -39,6 +40,44 @@ namespace XericLibrary.Runtime.MacroLibrary return res; } + + /// + /// ظϴ + /// + /// + /// + /// + /// + public static List FisherYatesShuffle(int length, int select, int repet) + { + if(select > length) + throw new ArgumentOutOfRangeException("ڽϴʱҪĿӵеĿ࣬Dzġ"); +#if UNITY_EDITOR + if(repet > 100) + Debug.LogError("ϴƵظ̫(> 100)"); +#endif + repet = Math.Clamp(repet, 1, 100); + + var random = new System.Random(); + + List arr = Enumerable.Range(0, length).ToList(); + List res = new List(); + + for(int j = 0; j < repet; j++) + { + for(int i = 0; i < length; i++) + { + int k = random.Next(arr.Count); + res.Add(arr[k]); + arr.RemoveAt(k); + } + arr = res.ToList(); + res.Clear(); + } + return arr; + } + + /// /// ˮس㷨̰߳ȫϴơ /// diff --git a/Runtime/MicroLibrary/MacroObject.cs b/Runtime/MicroLibrary/MacroObject.cs index 9e9b8bb..57c053f 100644 --- a/Runtime/MicroLibrary/MacroObject.cs +++ b/Runtime/MicroLibrary/MacroObject.cs @@ -99,19 +99,42 @@ namespace XericLibrary.Runtime.MacroLibrary /// public static IEnumerable GetChildren(this Transform trans) { +#if UNITY_EDITOR + if(trans == null) + throw new Exception("׼ȡӳԱʱڵΪ"); +#endif + for(int i = 0; i < trans.childCount; i++) { yield return trans.GetChild(i); } } + /// + /// ݹȡһ任ڵеӳԱ + /// + /// + /// ݹȣ1ʾڵһӼѰң2ʾڰһڵгԱѰ + /// + public static IEnumerable GetChildrenRecursion(this Transform trans, int recursion = 1) + { + int newdepth = recursion - 1; + foreach(var child in trans.GetChildren()) + { + if(newdepth > 0) + foreach(var item in child.GetChildrenRecursion(newdepth)) + yield return item; + yield return child; + } + } + /// /// ӳԱҵһ /// עֻвҡ /// /// /// - /// ȣ0ʾӼѰң1ʾӼѰңҲĽӼӼѰңԴ + /// ȣ1ʾӼѰң2ʾӼѰңҲĽӼӼѰңԴ /// public static IEnumerable GetComponentsInRecursionChildren(this Transform trans, int recursion = 1) where T : Component diff --git a/Runtime/MicroLibrary/MacroTransform.cs b/Runtime/MicroLibrary/MacroTransform.cs new file mode 100644 index 0000000..c402c4c --- /dev/null +++ b/Runtime/MicroLibrary/MacroTransform.cs @@ -0,0 +1,44 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace XericLibrary.Runtime.MacroLibrary +{ + /// + /// 任 + /// + public static class MacroTransform + { + + } + + + + /// + /// 任װ + /// + public class Conversion + { + #region ֶ + + /// + /// Ŀ任 + /// + private Transform transform; + + #endregion + + #region ʹ÷ + + public Conversion(Transform target) + { + transform = target; + } + + + #endregion + + + + } +} diff --git a/Runtime/MicroLibrary/MacroTransform.cs.meta b/Runtime/MicroLibrary/MacroTransform.cs.meta new file mode 100644 index 0000000..d4dddbf --- /dev/null +++ b/Runtime/MicroLibrary/MacroTransform.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0fab5c35f2f4c340aa0f55f743eec10 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/SpatialTransform/OffsetFromTrackTarget.cs b/Runtime/SpatialTransform/OffsetFromTrackTarget.cs index beb7cfc..158f40f 100644 --- a/Runtime/SpatialTransform/OffsetFromTrackTarget.cs +++ b/Runtime/SpatialTransform/OffsetFromTrackTarget.cs @@ -31,7 +31,6 @@ namespace XericLibrary.Runtime.SpatialTransform var trackPos = TrackTarget.position; trackPos.y = 0; - transform.position = TrackTarget.position + TrackTarget.rotation * Offset; transform.rotation = Quaternion.LookRotation(pos - trackPos); diff --git a/Runtime/Type/Quadtree.cs b/Runtime/Type/Quadtree.cs new file mode 100644 index 0000000..682240a --- /dev/null +++ b/Runtime/Type/Quadtree.cs @@ -0,0 +1,26 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace XericLibrary.Runtime.Type +{ + /// + /// IJ + /// + public class Quadtree : WeaklyObject + { + + } + + /// + /// IJڵ + /// + public class QuadtreeNode : WeaklyObject + { + #region ֶ + + + + #endregion + } +} \ No newline at end of file diff --git a/Runtime/Type/Quadtree.cs.meta b/Runtime/Type/Quadtree.cs.meta new file mode 100644 index 0000000..a86775b --- /dev/null +++ b/Runtime/Type/Quadtree.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8f8ad109e8af82e40bfad6047e7097ea +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Type/WeaklyHistoryMonoBase.cs b/Runtime/Type/WeaklyHistoryMonoBase.cs index e831349..d1212eb 100644 --- a/Runtime/Type/WeaklyHistoryMonoBase.cs +++ b/Runtime/Type/WeaklyHistoryMonoBase.cs @@ -13,36 +13,102 @@ namespace XericLibrary.Runtime.Type { #region ֶ - /// - /// ʷ - /// - private Vector3 history_Position; - - /// - /// ʷת - /// - private Quaternion history_Rotation; - - /// - /// ʷ - /// - private Vector3 history_Scale; - /// /// ȡʷ /// - public Vector3 HistoryPosition => history_Position; + public Vector3 HistoryPosition => last_LocalPosition; /// /// ȡʷת /// - public Quaternion HistoryRotation => history_Rotation; + public Quaternion HistoryRotation => last_LocalRotation; /// /// ȡʷ /// - public Vector3 HistoryScale => history_Scale; + public Vector3 HistoryScale => last_LocalScale; + + + + /// + /// ʼ + /// + private Vector3 initial_WorldPosition; + + /// + /// ʼת + /// + private Quaternion initial_WorldRotation; + + /// + /// һ֡ + /// + private Vector3 last_WorldPosition; + + /// + /// һ֡ת + /// + private Quaternion last_WorldRotation; + + /// + /// ʼ + /// + private Vector3 initial_LocalPosition; + + /// + /// ʼת + /// + private Quaternion initial_LocalRotation; + + /// + /// ʼ + /// + private Vector3 initial_LocalScale; + + /// + /// һ֡ + /// + private Vector3 last_LocalPosition; + + /// + /// һ֡ת + /// + private Quaternion last_LocalRotation; + + /// + /// һ֡ + /// + private Vector3 last_LocalScale; + + /// + /// ʹܸʷ任 + /// + private bool enableUpdateLastTransform; + #endregion + + #region + + protected virtual void Start() + { + initial_LocalPosition = transform.localPosition; + initial_LocalRotation = transform.localRotation; + initial_LocalScale = transform.localScale; + } + + protected virtual void Update() + { + + } + + #endregion + + #region ¼ + + + + #endregion + } } \ No newline at end of file