This commit is contained in:
2023-11-08 13:41:11 +08:00
parent 3b8bf5a940
commit d691f8cde8
24 changed files with 736 additions and 21 deletions
+3
View File
@@ -4,6 +4,9 @@
#### 版本 #### 版本
* new
添加编辑器阶段快捷生成、移除脚本
添加基本几何体拼接算法
* 0.2.1 * 0.2.1
修改:事件扩展 InvokeConservative 从ExtendType类中移到了MacroEvent 修改:事件扩展 InvokeConservative 从ExtendType类中移到了MacroEvent
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 076c2a8219fca8649bdb0bfae5a3aac5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5691b4e940f93e74cbac35f477e10af7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+219
View File
@@ -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<T> : WeaklyMonoBase
where T : WeaklyObject
{
#region 字段属性
/// <summary>
/// 生成器
/// </summary>
[SerializeField]
private T GenerationCompute;
#endregion
#region 生命周期
#endregion
#region 快捷静态
/// <summary>
/// 复制一个组件到另一个组件中
/// <code>
/// 这个方法只能用于 MonoBehaviours 或普通 C# 类的字段和属性,
/// 不能用于 Unity 内置组件(例如 Transform、Rigidbody 等)的复制,
/// 因为这些组件的字段和属性通常是私有的。
/// </code>
/// </summary>
/// <typeparam name="COMPONENT"></typeparam>
/// <param name="SOURCE"></param>
/// <param name="CopyTo"></param>
/// <returns></returns>
public static COMPONENT CopyComponentToAnother<COMPONENT>(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;
}
/// <summary>
/// 从给定的物体上克隆一个组件到新的物体上
/// <code>
/// 这个方法适用于所有类型的组件
/// </code>
/// </summary>
/// <typeparam name="COMPONENT"></typeparam>
/// <param name="source"></param>
/// <param name="copied"></param>
/// <returns></returns>
public static COMPONENT CopyComponentToNew<COMPONENT>(GameObject source, out GameObject copied)
where COMPONENT : Component
{
Component[] allComponents = source.GetComponents<Component>();
copied = Instantiate(source);
foreach(Component component in copied.GetComponents<Component>())
{
if(component.GetType() != typeof(COMPONENT))
{
#if UNITY_EDITOR
if(PlayModeState != PlayModeStateChange.EnteredPlayMode)
DestroyImmediate(component);
else
Destroy(component);
#else
Destroy(component);
#endif
}
}
return copied.GetComponent<COMPONENT>();
}
#endregion
#region 快捷方法
/// <summary>
/// 创建节点
/// </summary>
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;
}
/// <summary>
/// 使给定的游戏节点断子绝孙
/// </summary>
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
}
}
/// <summary>
/// 使给定的游戏节点断子绝孙
/// </summary>
protected void DieSonless(GameObject target)
{
DieSonless(target.transform);
}
/// <summary>
/// 提取所有组件,然后在同级中另起一个节点进行对指定组件结构的覆盖
/// </summary>
/// <typeparam name="COMPONENT"></typeparam>
/// <param name="transform">要提取组件的节点</param>
/// <param name="name">复制时另起节点的名称</param>
/// <param name="MaxStackDepth">最大检索深度</param>
protected void ExtractAllComponent<COMPONENT>(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<COMPONENT>(MaxStackDepth))
{
CreatOrGetGameNode($"component.{index++}", root.transform);
CopyComponentToNew<COMPONENT>(comp.gameObject, out var copiedObj);
copiedObj.transform.parent = root.transform;
}
}
#endregion
}
#if UNITY_EDITOR
/// <summary>
/// 基本的创建辅助器,用于在inspecter上创建按钮或其他功能
/// </summary>
public class BaseGenerateEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if(GUILayout.Button("生成更新"))
UpdateGen();
}
/// <summary>
/// 更新生成
/// </summary>
protected void UpdateGen()
{
var script = (StairsGenerate)target;
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d1b51df505c013648929cef9785bce3d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+73
View File
@@ -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
{
/// <summary>
/// 生成节点纯方法
/// </summary>
public class GenMethod : BaseGenerate<WeaklyObject>
{
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<Collider>(script.gameObject, out var outobj);
if(GUILayout.Button("销毁所有渲染组件"))
DestroyAllMesh(script.transform);
}
/// <summary>
/// 销毁所有模型组件
/// </summary>
public void DestroyAllMesh(Transform transform)
{
MeshFilter fl = null;
MeshRenderer mr;
Action<Component> 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<MeshFilter>();
destroycomponent(fl);
mr = item.GetComponent<MeshRenderer>();
destroycomponent(mr);
}
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d47c476cb54b0384f9a5ed853a81c637
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -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
{
/// <summary>
/// 创建楼梯
/// </summary>
public class StairsGenerate : BaseGenerate<StairsGeneration>
{
}
#if UNITY_EDITOR
[CustomEditor(typeof(StairsGenerate))]
public class ButtonUIGuideEditor : BaseGenerateEditor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// 在Inspector窗口上显示按钮
if(GUILayout.Button("静态浏览"))
{
}
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0f074c0e944db2143840f96a053a42e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 652abfb64baca2b489235a2124321cde
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,18 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XericLibrary.Runtime.Type;
namespace XericLibrary.Runtime.Generation
{
/// <summary>
/// Æ½ÃæÍÚ¿×Åöײ
/// </summary>
[Serializable]
public class PlaneWithHoleGeneration : WeaklyObject
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8024ed59544f0b346b46ff0e0d86457a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,18 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XericLibrary.Runtime.Type;
namespace XericLibrary.Runtime.Generation
{
/// <summary>
/// 矩形包围与穿孔,用于制作简易的房间合体
/// </summary>
[Serializable]
public class RectangularSurroundWithHoleGeneration : WeaklyObject
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0973eb189e2933541a254fc92cbb3476
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,32 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XericLibrary.Runtime.Type;
namespace XericLibrary.Runtime.Generation
{
/// <summary>
/// 生成楼梯简易碰撞;
/// 生成可循环的单段楼梯,比如固定步阶的楼梯
/// </summary>
[Serializable]
public class StairsGeneration : WeaklyObject
{
#region 字段属性
//public List<>
#endregion
#region 台阶参数
//public struct StairsParameter
//{
//}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 50cfea13c93ae974dbd744c96da5900d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+40 -1
View File
@@ -14,7 +14,8 @@ namespace XericLibrary.Runtime.MacroLibrary
#region 洗牌算法 #region 洗牌算法
/// <summary> /// <summary>
/// Fisher-Yates洗牌算法,时间复杂度为O(n*n),空间复杂度为O(n) /// Fisher-Yates洗牌算法,时间复杂度为O(n*n),空间复杂度为O(n);
/// 缺点是元素0很大概率会在最后出现
/// </summary> /// </summary>
/// <param name="length">待选项目的索引长度</param> /// <param name="length">待选项目的索引长度</param>
/// <param name="select">输出项目的索引长度</param> /// <param name="select">输出项目的索引长度</param>
@@ -39,6 +40,44 @@ namespace XericLibrary.Runtime.MacroLibrary
return res; return res;
} }
/// <summary>
/// 重复的洗牌
/// </summary>
/// <param name="length"></param>
/// <param name="select"></param>
/// <param name="repet"></param>
/// <returns></returns>
public static List<int> FisherYatesShuffle(int length, int select, int repet)
{
if(select > length)
throw new ArgumentOutOfRangeException("在进行洗牌时,要求输出的项目比拥有的项目更多,这是不合理的。");
#if UNITY_EDITOR
if(repet > 100)
Debug.LogError("洗牌的重复次数太高(> 100),已限制数量");
#endif
repet = Math.Clamp(repet, 1, 100);
var random = new System.Random();
List<int> arr = Enumerable.Range(0, length).ToList();
List<int> res = new List<int>();
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;
}
/// <summary> /// <summary>
/// 蓄水池抽样算法,可以在线程安全的情况下完成洗牌。 /// 蓄水池抽样算法,可以在线程安全的情况下完成洗牌。
/// </summary> /// </summary>
+24 -1
View File
@@ -99,19 +99,42 @@ namespace XericLibrary.Runtime.MacroLibrary
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Transform> GetChildren(this Transform trans) public static IEnumerable<Transform> GetChildren(this Transform trans)
{ {
#if UNITY_EDITOR
if(trans == null)
throw new Exception("在准备获取所有子成员时,节点为空");
#endif
for(int i = 0; i < trans.childCount; i++) for(int i = 0; i < trans.childCount; i++)
{ {
yield return trans.GetChild(i); yield return trans.GetChild(i);
} }
} }
/// <summary>
/// 递归获取一个变换节点下所有的子成员
/// </summary>
/// <param name="trans"></param>
/// <param name="recursion">递归深度,1表示仅在第一子集中寻找,2表示在包含下一级在内的所有成员中寻找</param>
/// <returns></returns>
public static IEnumerable<Transform> 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;
}
}
/// <summary> /// <summary>
/// 在所有子成员中深度优先找到第一个组件; /// 在所有子成员中深度优先找到第一个组件;
/// 注意这只会在子项中查找。 /// 注意这只会在子项中查找。
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="trans"></param> /// <param name="trans"></param>
/// <param name="recursion">遍历深度,0表示仅在子集中寻找,1表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param> /// <param name="recursion">遍历深度,1表示仅在子集中寻找,2表示在子集中寻找,如果找不到的将在子集的子集中寻找,以此类推</param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetComponentsInRecursionChildren<T>(this Transform trans, int recursion = 1) public static IEnumerable<T> GetComponentsInRecursionChildren<T>(this Transform trans, int recursion = 1)
where T : Component where T : Component
+44
View File
@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XericLibrary.Runtime.MacroLibrary
{
/// <summary>
/// 变换宏
/// </summary>
public static class MacroTransform
{
}
/// <summary>
/// 变换封装类
/// </summary>
public class Conversion
{
#region 字段属性
/// <summary>
/// 目标变换组件
/// </summary>
private Transform transform;
#endregion
#region 使用方法
public Conversion(Transform target)
{
transform = target;
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c0fab5c35f2f4c340aa0f55f743eec10
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -31,7 +31,6 @@ namespace XericLibrary.Runtime.SpatialTransform
var trackPos = TrackTarget.position; var trackPos = TrackTarget.position;
trackPos.y = 0; trackPos.y = 0;
transform.position = TrackTarget.position + TrackTarget.rotation * Offset; transform.position = TrackTarget.position + TrackTarget.rotation * Offset;
transform.rotation = Quaternion.LookRotation(pos - trackPos); transform.rotation = Quaternion.LookRotation(pos - trackPos);
+26
View File
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XericLibrary.Runtime.Type
{
/// <summary>
/// 四叉树
/// </summary>
public class Quadtree : WeaklyObject
{
}
/// <summary>
/// 四叉树节点
/// </summary>
public class QuadtreeNode : WeaklyObject
{
#region 字段属性
#endregion
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f8ad109e8af82e40bfad6047e7097ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+84 -18
View File
@@ -13,36 +13,102 @@ namespace XericLibrary.Runtime.Type
{ {
#region 字段属性 #region 字段属性
/// <summary>
/// 历史坐标
/// </summary>
private Vector3 history_Position;
/// <summary>
/// 历史旋转
/// </summary>
private Quaternion history_Rotation;
/// <summary>
/// 历史缩放
/// </summary>
private Vector3 history_Scale;
/// <summary> /// <summary>
/// 获取历史坐标 /// 获取历史坐标
/// </summary> /// </summary>
public Vector3 HistoryPosition => history_Position; public Vector3 HistoryPosition => last_LocalPosition;
/// <summary> /// <summary>
/// 获取历史旋转 /// 获取历史旋转
/// </summary> /// </summary>
public Quaternion HistoryRotation => history_Rotation; public Quaternion HistoryRotation => last_LocalRotation;
/// <summary> /// <summary>
/// 获取历史缩放 /// 获取历史缩放
/// </summary> /// </summary>
public Vector3 HistoryScale => history_Scale; public Vector3 HistoryScale => last_LocalScale;
/// <summary>
/// 初始世界坐标
/// </summary>
private Vector3 initial_WorldPosition;
/// <summary>
/// 初始世界旋转
/// </summary>
private Quaternion initial_WorldRotation;
/// <summary>
/// 上一帧世界坐标
/// </summary>
private Vector3 last_WorldPosition;
/// <summary>
/// 上一帧世界旋转
/// </summary>
private Quaternion last_WorldRotation;
/// <summary>
/// 初始坐标
/// </summary>
private Vector3 initial_LocalPosition;
/// <summary>
/// 初始旋转
/// </summary>
private Quaternion initial_LocalRotation;
/// <summary>
/// 初始缩放
/// </summary>
private Vector3 initial_LocalScale;
/// <summary>
/// 上一帧坐标
/// </summary>
private Vector3 last_LocalPosition;
/// <summary>
/// 上一帧旋转
/// </summary>
private Quaternion last_LocalRotation;
/// <summary>
/// 上一帧缩放
/// </summary>
private Vector3 last_LocalScale;
/// <summary>
/// 使能更新历史变换
/// </summary>
private bool enableUpdateLastTransform;
#endregion #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
} }
} }