框架上传
This commit is contained in:
3
Assets/00.StaryEvo/Runtime/Tool/FluentAPI/0.Unity.meta
Normal file
3
Assets/00.StaryEvo/Runtime/Tool/FluentAPI/0.Unity.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84dfa4614a74466f938b3c4cfb7f63f0
|
||||
timeCreated: 1647443763
|
||||
@@ -0,0 +1,230 @@
|
||||
using Stary.Evo;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Object", 0)]
|
||||
[APIDescriptionCN("针对 UnityEngine.Object 提供的链式扩展")]
|
||||
[APIDescriptionEN("The chain extension provided by UnityEngine.Object")]
|
||||
[APIExampleCode(@"
|
||||
var gameObject = new GameObject();
|
||||
//
|
||||
gameObject.Instantiate()
|
||||
.Name(""ExtensionExample"")
|
||||
.DestroySelf();
|
||||
//
|
||||
gameObject.Instantiate()
|
||||
.DestroySelfGracefully();
|
||||
//
|
||||
gameObject.Instantiate()
|
||||
.DestroySelfAfterDelay(1.0f);
|
||||
//
|
||||
gameObject.Instantiate()
|
||||
.DestroySelfAfterDelayGracefully(1.0f);
|
||||
//
|
||||
gameObject
|
||||
.Self(selfObj => Debug.Log(selfObj.name))
|
||||
.Name(""TestObj"")
|
||||
.Self(selfObj => Debug.Log(selfObj.name))
|
||||
.Name(""ExtensionExample"")
|
||||
.DontDestroyOnLoad();
|
||||
")]
|
||||
#endif
|
||||
public static class UnityEngineObjectExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.37
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Instantiate(Object) 的简单链式封装")]
|
||||
[APIDescriptionEN("Object.Instantiate(Object) extension")]
|
||||
[APIExampleCode(@"
|
||||
prefab.Instantiate();
|
||||
")]
|
||||
#endif
|
||||
public static T Instantiate<T>(this T selfObj) where T : UnityEngine.Object
|
||||
{
|
||||
return UnityEngine.Object.Instantiate(selfObj);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.38
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Instantiate(Object,Vector3,Quaternion) 的简单链式封装")]
|
||||
[APIDescriptionEN("Object.Instantiate(Object,Vector3,Quaternion) extension")]
|
||||
[APIExampleCode(@"
|
||||
prefab.Instantiate(Vector3.zero,Quaternion.identity);
|
||||
")]
|
||||
#endif
|
||||
public static T Instantiate<T>(this T selfObj, Vector3 position, Quaternion rotation)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
return UnityEngine.Object.Instantiate(selfObj, position, rotation);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.39
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Instantiate(Object,Vector3,Quaternion,Transform parent) 的简单链式封装")]
|
||||
[APIDescriptionEN("Object.Instantiate(Object,Vector3,Quaternion,Transform parent) extension")]
|
||||
[APIExampleCode(@"
|
||||
prefab.Instantiate(Vector3.zero,Quaternion.identity,transformRoot);
|
||||
")]
|
||||
#endif
|
||||
public static T Instantiate<T>(
|
||||
this T selfObj,
|
||||
Vector3 position,
|
||||
Quaternion rotation,
|
||||
Transform parent)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
return UnityEngine.Object.Instantiate(selfObj, position, rotation, parent);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.40
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Instantiate(Transform parent,bool worldPositionStays) 的简单链式封装")]
|
||||
[APIDescriptionEN("Object.Instantiate(Transform parent,bool worldPositionStays) extension")]
|
||||
[APIExampleCode(@"
|
||||
prefab.Instantiate(transformRoot,true);
|
||||
")]
|
||||
#endif
|
||||
public static T InstantiateWithParent<T>(this T selfObj, Transform parent, bool worldPositionStays)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
return (T)UnityEngine.Object.Instantiate((UnityEngine.Object)selfObj, parent, worldPositionStays);
|
||||
}
|
||||
|
||||
public static T InstantiateWithParent<T>(this T selfObj, Component parent, bool worldPositionStays)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
return (T)UnityEngine.Object.Instantiate((UnityEngine.Object)selfObj, parent.transform, worldPositionStays);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.41
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Instantiate(Transform parent) 的简单链式封装")]
|
||||
[APIDescriptionEN("Object.Instantiate(Transform parent) extension")]
|
||||
[APIExampleCode(@"
|
||||
prefab.Instantiate(transformRoot);
|
||||
")]
|
||||
#endif
|
||||
public static T InstantiateWithParent<T>(this T selfObj, Transform parent) where T : UnityEngine.Object
|
||||
{
|
||||
return UnityEngine.Object.Instantiate(selfObj, parent, false);
|
||||
}
|
||||
|
||||
public static T InstantiateWithParent<T>(this T selfObj, Component parent) where T : UnityEngine.Object
|
||||
{
|
||||
return UnityEngine.Object.Instantiate(selfObj, parent.transform, false);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.42
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("设置名字")]
|
||||
[APIDescriptionEN("set Object's name")]
|
||||
[APIExampleCode(@"
|
||||
scriptableObject.Name(""LevelData"");
|
||||
Debug.Log(scriptableObject.name);
|
||||
// LevelData
|
||||
")]
|
||||
#endif
|
||||
public static T Name<T>(this T selfObj, string name) where T : UnityEngine.Object
|
||||
{
|
||||
selfObj.name = name;
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.43
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Destroy(Object) 简单链式封装")]
|
||||
[APIDescriptionEN("Object.Destroy(Object) extension")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().DestroySelf()
|
||||
")]
|
||||
#endif
|
||||
public static void DestroySelf<T>(this T selfObj) where T : UnityEngine.Object
|
||||
{
|
||||
UnityEngine.Object.Destroy(selfObj);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.44
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Destroy(Object) 简单链式封装")]
|
||||
[APIDescriptionEN("Object.Destroy(Object) extension")]
|
||||
[APIExampleCode(@"
|
||||
GameObject gameObj = null;
|
||||
gameObj.DestroySelfGracefully();
|
||||
// not throw null exception
|
||||
// 这样写不会报异常(但是不好调试)
|
||||
")]
|
||||
#endif
|
||||
public static T DestroySelfGracefully<T>(this T selfObj) where T : UnityEngine.Object
|
||||
{
|
||||
if (selfObj)
|
||||
{
|
||||
UnityEngine.Object.Destroy(selfObj);
|
||||
}
|
||||
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.45
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Destroy(Object,float) 简单链式封装")]
|
||||
[APIDescriptionEN("Object.Destroy(Object,float) extension")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().DestroySelfAfterDelay(5);
|
||||
")]
|
||||
#endif
|
||||
public static T DestroySelfAfterDelay<T>(this T selfObj, float afterDelay) where T : UnityEngine.Object
|
||||
{
|
||||
UnityEngine.Object.Destroy(selfObj, afterDelay);
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.46
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Destroy(Object,float) 简单链式封装")]
|
||||
[APIDescriptionEN("Object.Destroy(Object,float) extension")]
|
||||
[APIExampleCode(@"
|
||||
GameObject gameObj = null;
|
||||
gameObj.DestroySelfAfterDelayGracefully(5);
|
||||
// not throw exception
|
||||
// 不会报异常
|
||||
")]
|
||||
#endif
|
||||
public static T DestroySelfAfterDelayGracefully<T>(this T selfObj, float delay) where T : UnityEngine.Object
|
||||
{
|
||||
if (selfObj)
|
||||
{
|
||||
UnityEngine.Object.Destroy(selfObj, delay);
|
||||
}
|
||||
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.47
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.DontDestroyOnLoad 简单链式封装")]
|
||||
[APIDescriptionEN("Object.DontDestroyOnLoad extension")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().DontDestroyOnLoad();
|
||||
")]
|
||||
#endif
|
||||
public static T DontDestroyOnLoad<T>(this T selfObj) where T : UnityEngine.Object
|
||||
{
|
||||
UnityEngine.Object.DontDestroyOnLoad(selfObj);
|
||||
return selfObj;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f82c097d6fc40e3ba52585f53920178
|
||||
timeCreated: 1647487579
|
||||
@@ -0,0 +1,334 @@
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.GameObject", 1)]
|
||||
[APIDescriptionCN("针对 UnityEngine.GameObject 提供的链式扩展")]
|
||||
[APIDescriptionEN("The chain extension provided by UnityEngine.Object.")]
|
||||
[APIExampleCode(@"
|
||||
var gameObject = new GameObject();
|
||||
var transform = gameObject.transform;
|
||||
var selfScript = gameObject.AddComponent<MonoBehaviour>();
|
||||
var boxCollider = gameObject.AddComponent<BoxCollider>();
|
||||
//
|
||||
gameObject.Show(); // gameObject.SetActive(true)
|
||||
selfScript.Show(); // this.gameObject.SetActive(true)
|
||||
boxCollider.Show(); // boxCollider.gameObject.SetActive(true)
|
||||
gameObject.transform.Show(); // transform.gameObject.SetActive(true)
|
||||
//
|
||||
gameObject.Hide(); // gameObject.SetActive(false)
|
||||
selfScript.Hide(); // this.gameObject.SetActive(false)
|
||||
boxCollider.Hide(); // boxCollider.gameObject.SetActive(false)
|
||||
transform.Hide(); // transform.gameObject.SetActive(false)
|
||||
//
|
||||
selfScript.DestroyGameObj();
|
||||
boxCollider.DestroyGameObj();
|
||||
]transform.DestroyGameObj();
|
||||
//
|
||||
selfScript.DestroyGameObjGracefully();
|
||||
boxCollider.DestroyGameObjGracefully();
|
||||
transform.DestroyGameObjGracefully();
|
||||
//
|
||||
selfScript.DestroyGameObjAfterDelay(1.0f);
|
||||
boxCollider.DestroyGameObjAfterDelay(1.0f);
|
||||
transform.DestroyGameObjAfterDelay(1.0f);
|
||||
//
|
||||
selfScript.DestroyGameObjAfterDelayGracefully(1.0f);
|
||||
boxCollider.DestroyGameObjAfterDelayGracefully(1.0f);
|
||||
transform.DestroyGameObjAfterDelayGracefully(1.0f);
|
||||
//
|
||||
gameObject.Layer(0);
|
||||
selfScript.Layer(0);
|
||||
boxCollider.Layer(0);
|
||||
transform.Layer(0);
|
||||
//
|
||||
gameObject.Layer(""Default"");
|
||||
selfScript.Layer(""Default"");
|
||||
boxCollider.Layer(""Default"");
|
||||
transform.Layer(""Default"");
|
||||
")]
|
||||
#endif
|
||||
public static class UnityEngineGameObjectExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.48
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("gameObject.SetActive(true)")]
|
||||
[APIDescriptionEN("gameObject.SetActive(true)")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().Show();
|
||||
")]
|
||||
#endif
|
||||
public static GameObject Show(this GameObject selfObj)
|
||||
{
|
||||
selfObj.SetActive(true);
|
||||
return selfObj;
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.49
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("script.gameObject.SetActive(true)")]
|
||||
[APIDescriptionEN("script.gameObject.SetActive(true)")]
|
||||
[APIExampleCode(@"
|
||||
GetComponent<MyScript>().Show();
|
||||
")]
|
||||
#endif
|
||||
public static T Show<T>(this T selfComponent) where T : Component
|
||||
{
|
||||
selfComponent.gameObject.Show();
|
||||
return selfComponent;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.50
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("gameObject.SetActive(false)")]
|
||||
[APIDescriptionEN("gameObject.SetActive(false)")]
|
||||
[APIExampleCode(@"
|
||||
gameObject.Hide();
|
||||
")]
|
||||
#endif
|
||||
public static GameObject Hide(this GameObject selfObj)
|
||||
{
|
||||
selfObj.SetActive(false);
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.51
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("myScript.gameObject.SetActive(false)")]
|
||||
[APIDescriptionEN("myScript.gameObject.SetActive(false)")]
|
||||
[APIExampleCode(@"
|
||||
GetComponent<MyScript>().Hide();
|
||||
")]
|
||||
#endif
|
||||
public static T Hide<T>(this T selfComponent) where T : Component
|
||||
{
|
||||
selfComponent.gameObject.Hide();
|
||||
return selfComponent;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.52
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Destroy(myScript.gameObject)")]
|
||||
[APIDescriptionEN("Destroy(myScript.gameObject)")]
|
||||
[APIExampleCode(@"
|
||||
myScript.DestroyGameObj();
|
||||
")]
|
||||
#endif
|
||||
public static void DestroyGameObj<T>(this T selfBehaviour) where T : Component
|
||||
{
|
||||
selfBehaviour.gameObject.DestroySelf();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.53
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("if (myScript) Destroy(myScript.gameObject)")]
|
||||
[APIDescriptionEN("if (myScript) Destroy(myScript.gameObject)")]
|
||||
[APIExampleCode(@"
|
||||
myScript.DestroyGameObjGracefully();
|
||||
")]
|
||||
#endif
|
||||
public static void DestroyGameObjGracefully<T>(this T selfBehaviour) where T : Component
|
||||
{
|
||||
if (selfBehaviour && selfBehaviour.gameObject)
|
||||
{
|
||||
selfBehaviour.gameObject.DestroySelfGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.54
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Object.Destroy(myScript.gameObject,delaySeconds)")]
|
||||
[APIDescriptionEN("Object.Destroy(myScript.gameObject,delaySeconds)")]
|
||||
[APIExampleCode(@"
|
||||
myScript.DestroyGameObjAfterDelay(5);
|
||||
")]
|
||||
#endif
|
||||
public static T DestroyGameObjAfterDelay<T>(this T selfBehaviour, float delay) where T : Component
|
||||
{
|
||||
selfBehaviour.gameObject.DestroySelfAfterDelay(delay);
|
||||
return selfBehaviour;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.55
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("if (myScript && myScript.gameObject) Object.Destroy(myScript.gameObject,delaySeconds)")]
|
||||
[APIDescriptionEN("if (myScript && myScript.gameObject) Object.Destroy(myScript.gameObject,delaySeconds)")]
|
||||
[APIExampleCode(@"
|
||||
myScript.DestroyGameObjAfterDelayGracefully(5);
|
||||
")]
|
||||
#endif
|
||||
public static T DestroyGameObjAfterDelayGracefully<T>(this T selfBehaviour, float delay) where T : Component
|
||||
{
|
||||
if (selfBehaviour && selfBehaviour.gameObject)
|
||||
{
|
||||
selfBehaviour.gameObject.DestroySelfAfterDelay(delay);
|
||||
}
|
||||
|
||||
return selfBehaviour;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.56
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("gameObject.layer = layer")]
|
||||
[APIDescriptionEN("gameObject.layer = layer")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().Layer(0);
|
||||
")]
|
||||
#endif
|
||||
public static GameObject Layer(this GameObject selfObj, int layer)
|
||||
{
|
||||
selfObj.layer = layer;
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.57
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("component.gameObject.layer = layer")]
|
||||
[APIDescriptionEN("component.gameObject.layer = layer")]
|
||||
[APIExampleCode(@"
|
||||
rigidbody2D.Layer(0);
|
||||
")]
|
||||
#endif
|
||||
public static T Layer<T>(this T selfComponent, int layer) where T : Component
|
||||
{
|
||||
selfComponent.gameObject.layer = layer;
|
||||
return selfComponent;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.58
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("gameObj.layer = LayerMask.NameToLayer(layerName)")]
|
||||
[APIDescriptionEN("gameObj.layer = LayerMask.NameToLayer(layerName)")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject().Layer(""Default"");
|
||||
")]
|
||||
#endif
|
||||
|
||||
public static GameObject Layer(this GameObject selfObj, string layerName)
|
||||
{
|
||||
selfObj.layer = LayerMask.NameToLayer(layerName);
|
||||
return selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.59
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("component.gameObject.layer = LayerMask.NameToLayer(layerName)")]
|
||||
[APIDescriptionEN("component.gameObject.layer = LayerMask.NameToLayer(layerName)")]
|
||||
[APIExampleCode(@"
|
||||
spriteRenderer.Layer(""Default"");
|
||||
")]
|
||||
#endif
|
||||
public static T Layer<T>(this T selfComponent, string layerName) where T : Component
|
||||
{
|
||||
selfComponent.gameObject.layer = LayerMask.NameToLayer(layerName);
|
||||
return selfComponent;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.60
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("layerMask 中的层级是否包含 gameObj 所在的层级")]
|
||||
[APIDescriptionEN("Whether the layer in layerMask contains the same layer as gameObj")]
|
||||
[APIExampleCode(@"
|
||||
gameObj.IsInLayerMask(layerMask);
|
||||
")]
|
||||
#endif
|
||||
public static bool IsInLayerMask(this GameObject selfObj, LayerMask layerMask)
|
||||
{
|
||||
return LayerMaskUtility.IsInLayerMask(selfObj, layerMask);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.61
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("layerMask 中的层级是否包含 component.gameObject 所在的层级")]
|
||||
[APIDescriptionEN("Whether the layer in layerMask contains the same layer as component.gameObject")]
|
||||
[APIExampleCode(@"
|
||||
spriteRenderer.IsInLayerMask(layerMask);
|
||||
")]
|
||||
#endif
|
||||
public static bool IsInLayerMask<T>(this T selfComponent, LayerMask layerMask) where T : Component
|
||||
{
|
||||
return LayerMaskUtility.IsInLayerMask(selfComponent.gameObject, layerMask);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.62
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取组件,没有则添加再返回")]
|
||||
[APIDescriptionEN("Get component, add and return if not")]
|
||||
[APIExampleCode(@"
|
||||
gameObj.GetOrAddComponent<SpriteRenderer>();
|
||||
")]
|
||||
#endif
|
||||
public static T GetOrAddComponent<T>(this GameObject self) where T : Component
|
||||
{
|
||||
var comp = self.gameObject.GetComponent<T>();
|
||||
return comp ? comp : self.gameObject.AddComponent<T>();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.63
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取组件,没有则添加再返回")]
|
||||
[APIDescriptionEN("Get component, add and return if not")]
|
||||
[APIExampleCode(@"
|
||||
component.GetOrAddComponent<SpriteRenderer>();
|
||||
")]
|
||||
#endif
|
||||
public static T GetOrAddComponent<T>(this Component component) where T : Component
|
||||
{
|
||||
return component.gameObject.GetOrAddComponent<T>();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.64
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取组件,没有则添加再返回")]
|
||||
[APIDescriptionEN("Get component, add and return if not")]
|
||||
[APIExampleCode(@"
|
||||
gameObj.GetOrAddComponent(typeof(SpriteRenderer));
|
||||
")]
|
||||
#endif
|
||||
public static Component GetOrAddComponent(this GameObject self, Type type)
|
||||
{
|
||||
var component = self.gameObject.GetComponent(type);
|
||||
return component ? component : self.gameObject.AddComponent(type);
|
||||
}
|
||||
}
|
||||
|
||||
public static class LayerMaskUtility
|
||||
{
|
||||
public static bool IsInLayerMask(int layer, LayerMask layerMask)
|
||||
{
|
||||
var objLayerMask = 1 << layer;
|
||||
return (layerMask.value & objLayerMask) == objLayerMask;
|
||||
}
|
||||
|
||||
public static bool IsInLayerMask(GameObject gameObj, LayerMask layerMask)
|
||||
{
|
||||
// 根据Layer数值进行移位获得用于运算的Mask值
|
||||
var objLayerMask = 1 << gameObj.layer;
|
||||
return (layerMask.value & objLayerMask) == objLayerMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36146f65e25b4ec79d3030f3babbc6cc
|
||||
timeCreated: 1647490642
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4b2070a7f53455e8ecc24c4d79c0a22
|
||||
timeCreated: 1647492399
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.MonoBehaviour", 3)]
|
||||
[APIDescriptionCN("MonoBehaviour 静态扩展")]
|
||||
[APIDescriptionEN("MonoBehaviour extension")]
|
||||
#endif
|
||||
public static class UnityEngineMonoBehaviourExtension
|
||||
{
|
||||
public static void Example()
|
||||
{
|
||||
var gameObject = new GameObject();
|
||||
var component = gameObject.GetComponent<MonoBehaviour>();
|
||||
|
||||
component.Enable(); // component.enabled = true
|
||||
component.Disable(); // component.enabled = false
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.149
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("monoBehaviour.enable = true")]
|
||||
[APIDescriptionEN("monoBehaviour.enable = true)")]
|
||||
[APIExampleCode(@"
|
||||
myScript.Enable();
|
||||
")]
|
||||
#endif
|
||||
public static T Enable<T>(this T selfBehaviour, bool enable = true) where T : Behaviour
|
||||
{
|
||||
selfBehaviour.enabled = enable;
|
||||
return selfBehaviour;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.150
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("monoBehaviour.enable = false")]
|
||||
[APIDescriptionEN("monoBehaviour.enable = false")]
|
||||
[APIExampleCode(@"
|
||||
myScript.Disable();
|
||||
")]
|
||||
#endif
|
||||
public static T Disable<T>(this T selfBehaviour) where T : Behaviour
|
||||
{
|
||||
selfBehaviour.enabled = false;
|
||||
return selfBehaviour;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8de3fdd3a4964430b98cbc29c78e56f3
|
||||
timeCreated: 1647507236
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Camera", 4)]
|
||||
[APIDescriptionCN("UnityEngine.Camera 静态扩展")]
|
||||
[APIDescriptionEN("UnityEngine.Camera extension")]
|
||||
#endif
|
||||
public static class UnityEngineCameraExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.151
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("截图")]
|
||||
[APIDescriptionEN("captureScreen")]
|
||||
[APIExampleCode(@"
|
||||
Camera.main.CaptureCamera(new Rect(0, 0, Screen.width, Screen.height));
|
||||
")]
|
||||
#endif
|
||||
public static Texture2D CaptureCamera(this Camera camera, Rect rect)
|
||||
{
|
||||
var renderTexture = new RenderTexture(Screen.width, Screen.height, 0);
|
||||
camera.targetTexture = renderTexture;
|
||||
camera.Render();
|
||||
|
||||
RenderTexture.active = renderTexture;
|
||||
|
||||
var screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
|
||||
screenShot.ReadPixels(rect, 0, 0);
|
||||
screenShot.Apply();
|
||||
|
||||
camera.targetTexture = null;
|
||||
RenderTexture.active = null;
|
||||
UnityEngine.Object.Destroy(renderTexture);
|
||||
|
||||
return screenShot;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 20fcd7ffbe7a4e6eb0a874c17e9d20ac
|
||||
timeCreated: 1647507743
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Color", 5)]
|
||||
[APIDescriptionCN("UnityEngine.Color 静态扩展")]
|
||||
[APIDescriptionEN("UnityEngine.Color extension")]
|
||||
#endif
|
||||
public static class UnityEngineColorExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.152
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("HTML string(#000000) 转 Color")]
|
||||
[APIDescriptionEN("HTML string(like #000000)")]
|
||||
[APIExampleCode(@"
|
||||
var color = ""#C5563CFF"".HtmlStringToColor();
|
||||
Debug.Log(color);"
|
||||
)]
|
||||
#endif
|
||||
public static Color HtmlStringToColor(this string htmlString)
|
||||
{
|
||||
var parseSucceed = ColorUtility.TryParseHtmlString(htmlString, out var retColor);
|
||||
return parseSucceed ? retColor : Color.black;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 373d8df28d2643bc823a4326ba396126
|
||||
timeCreated: 1647507950
|
||||
@@ -0,0 +1,54 @@
|
||||
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Graphic", 6)]
|
||||
[APIDescriptionCN("UnityEngine.UI.Graphic 静态扩展")]
|
||||
[APIDescriptionEN("UnityEngine.UI.Graphic extension")]
|
||||
#endif
|
||||
public static class UnityEngineUIGraphicExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.153
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("设置 Graphic 的 alpha 值 ")]
|
||||
[APIDescriptionEN("set graphic's alpha value")]
|
||||
[APIExampleCode(@"
|
||||
var gameObject = new GameObject();
|
||||
var image = gameObject.AddComponent<Image>();
|
||||
var rawImage = gameObject.AddComponent<RawImage>();
|
||||
|
||||
|
||||
image.ColorAlpha(1.0f);
|
||||
rawImage.ColorAlpha(1.0f);
|
||||
")]
|
||||
#endif
|
||||
public static T ColorAlpha<T>(this T selfGraphic, float alpha) where T : Graphic
|
||||
{
|
||||
var color = selfGraphic.color;
|
||||
color.a = alpha;
|
||||
selfGraphic.color = color;
|
||||
return selfGraphic;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.154
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("设置 image 的 fillAmount 值")]
|
||||
[APIDescriptionEN("set image's fillAmount value")]
|
||||
[APIExampleCode(@"
|
||||
var gameObject = new GameObject();
|
||||
var image1 = gameObject.AddComponent<Image>();
|
||||
|
||||
image1.FillAmount(0.0f);
|
||||
")]
|
||||
#endif
|
||||
public static Image FillAmount(this Image selfImage, float fillAmount)
|
||||
{
|
||||
selfImage.fillAmount = fillAmount;
|
||||
return selfImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ffdcff2f9c014047bc785723bc292477
|
||||
timeCreated: 1647508258
|
||||
@@ -0,0 +1,321 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Others", 8)]
|
||||
[APIDescriptionCN("其他的一些静态扩展")]
|
||||
[APIDescriptionEN("other extension")]
|
||||
#endif
|
||||
public static class UnityEngineOthersExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.155
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("随机 List 中的一个元素")]
|
||||
[APIDescriptionEN("get random item in a list")]
|
||||
[APIExampleCode(@"
|
||||
new List<int>(){ 1,2,3 }.GetRandomItem();
|
||||
")]
|
||||
#endif
|
||||
public static T GetRandomItem<T>(this List<T> list)
|
||||
{
|
||||
return list[UnityEngine.Random.Range(0, list.Count)];
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.34
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("随机获取并删除 List 中的一个元素")]
|
||||
[APIDescriptionEN("get and remove random item in a list")]
|
||||
[APIExampleCode(@"
|
||||
new List<int>(){ 1,2,3 }.GetAndRemoveRandomItem();
|
||||
")]
|
||||
#endif
|
||||
public static T GetAndRemoveRandomItem<T>(this List<T> list)
|
||||
{
|
||||
var randomIndex = UnityEngine.Random.Range(0, list.Count);
|
||||
var randomItem = list[randomIndex];
|
||||
list.RemoveAt(randomIndex);
|
||||
return randomItem;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("为 SpriteRender 设置 alpha 值")]
|
||||
[APIDescriptionEN("set SpriteRender's alpha value")]
|
||||
[APIExampleCode(@"
|
||||
mySprRender.Alpha(0.5f);
|
||||
")]
|
||||
#endif
|
||||
public static SpriteRenderer Alpha(this SpriteRenderer self, float alpha)
|
||||
{
|
||||
var color = self.color;
|
||||
color.a = alpha;
|
||||
self.color = color;
|
||||
return self;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.31
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Lerp")]
|
||||
[APIDescriptionEN("Mathf.Lerp")]
|
||||
[APIExampleCode(@"
|
||||
var v = 0.5f.Lerp(0.1f,0.5f);
|
||||
// v is 0.3f
|
||||
")]
|
||||
#endif
|
||||
public static float Lerp(this float self, float a, float b)
|
||||
{
|
||||
return Mathf.Lerp(a, b, self);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.31
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Abs")]
|
||||
[APIDescriptionEN("Mathf.Abs")]
|
||||
[APIExampleCode(@"
|
||||
var absValue = -1.0f.Abs();
|
||||
// absValue is 1.0f
|
||||
")]
|
||||
#endif
|
||||
public static float Abs(this float self)
|
||||
{
|
||||
return Mathf.Abs(self);
|
||||
}
|
||||
|
||||
public static float Abs(this int self)
|
||||
{
|
||||
return Mathf.Abs(self);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.150
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Exp")]
|
||||
[APIDescriptionEN("Mathf.Exp")]
|
||||
[APIExampleCode(@"
|
||||
var expValue = 1.0f.Exp(); // Mathf.Exp(1.0f)
|
||||
")]
|
||||
#endif
|
||||
public static float Exp(this float self)
|
||||
{
|
||||
return Mathf.Exp(self);
|
||||
}
|
||||
|
||||
public static float Exp(this int self)
|
||||
{
|
||||
return Mathf.Exp(self);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.31
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Sign")]
|
||||
[APIDescriptionEN("Mathf.Sign")]
|
||||
[APIExampleCode(@"
|
||||
var sign = -5.0f.Sign();
|
||||
// sign is 5.0f
|
||||
")]
|
||||
#endif
|
||||
public static float Sign(this float self)
|
||||
{
|
||||
return Mathf.Sign(self);
|
||||
}
|
||||
|
||||
public static float Sign(this int self)
|
||||
{
|
||||
return Mathf.Sign(self);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Cos")]
|
||||
[APIDescriptionEN("Mathf.Cos")]
|
||||
[APIExampleCode(@"
|
||||
var cos = (90.0f * Mathf.Deg2Rad).Cos();
|
||||
// cos is 0f
|
||||
")]
|
||||
#endif
|
||||
public static float Cos(this float self)
|
||||
{
|
||||
return Mathf.Cos(self);
|
||||
}
|
||||
|
||||
public static float Cos(this int self)
|
||||
{
|
||||
return Mathf.Cos(self);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Sin")]
|
||||
[APIDescriptionEN("Mathf.Sin")]
|
||||
[APIExampleCode(@"
|
||||
var sin = (90.0f * Mathf.Deg2Rad).Sin();
|
||||
// sin is 1f
|
||||
")]
|
||||
#endif
|
||||
public static float Sin(this float self)
|
||||
{
|
||||
return Mathf.Sin(self);
|
||||
}
|
||||
|
||||
public static float Sin(this int self)
|
||||
{
|
||||
return Mathf.Sin(self);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Cos(x * Mathf.Deg2Rad)")]
|
||||
[APIDescriptionEN("Mathf.Cos(x * Mathf.Deg2Rad)")]
|
||||
[APIExampleCode(@"
|
||||
var cos = 90.0f.CosAngle();
|
||||
// cos is 0f
|
||||
")]
|
||||
#endif
|
||||
public static float CosAngle(this float self)
|
||||
{
|
||||
return Mathf.Cos(self * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
public static float CosAngle(this int self)
|
||||
{
|
||||
return Mathf.Cos(self * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Sin(x * Mathf.Deg2Rad)")]
|
||||
[APIDescriptionEN("Mathf.Sin(x * Mathf.Deg2Rad)")]
|
||||
[APIExampleCode(@"
|
||||
var sin = 90.0f.SinAngle();
|
||||
// sin is 1f
|
||||
")]
|
||||
#endif
|
||||
public static float SinAngle(this float self)
|
||||
{
|
||||
return Mathf.Sin(self * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
public static float SinAngle(this int self)
|
||||
{
|
||||
return Mathf.Sin(self * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Deg2Rad")]
|
||||
[APIDescriptionEN("Mathf.Deg2Rad")]
|
||||
[APIExampleCode(@"
|
||||
var radius = 90.0f.Deg2Rad();
|
||||
// radius is 1.57f
|
||||
")]
|
||||
#endif
|
||||
public static float Deg2Rad(this float self)
|
||||
{
|
||||
return self * Mathf.Deg2Rad;
|
||||
}
|
||||
|
||||
public static float Deg2Rad(this int self)
|
||||
{
|
||||
return self * Mathf.Deg2Rad;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("Mathf.Rad2Deg")]
|
||||
[APIDescriptionEN("Mathf.Rad2Deg")]
|
||||
[APIExampleCode(@"
|
||||
var degree = 1.57f.Rad2Deg();
|
||||
// degree is 90f
|
||||
")]
|
||||
#endif
|
||||
public static float Rad2Deg(this float self)
|
||||
{
|
||||
return self * Mathf.Rad2Deg;
|
||||
}
|
||||
|
||||
public static float Rad2Deg(this int self)
|
||||
{
|
||||
return self * Mathf.Rad2Deg;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.129
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将欧拉角转换为方向向量(Vector2)")]
|
||||
[APIDescriptionEN("Convert Degree To Direction(Vector2)")]
|
||||
[APIExampleCode(@"
|
||||
var direction = 90.AngleToDirection2D();
|
||||
// Vector2(1,0)
|
||||
")]
|
||||
#endif
|
||||
|
||||
public static Vector2 AngleToDirection2D(this int self)
|
||||
{
|
||||
return new Vector2(self.CosAngle(), self.SinAngle());
|
||||
}
|
||||
|
||||
public static Vector2 AngleToDirection2D(this float self)
|
||||
{
|
||||
return new Vector2(self.CosAngle(), self.SinAngle());
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Added in v1.0.129
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将方向(Vector2)转换为欧拉角")]
|
||||
[APIDescriptionEN("Convert Direction To Degrees")]
|
||||
[APIExampleCode(@"
|
||||
var direction = Vector2.right.ToAngle();
|
||||
// Vector2(1,0)
|
||||
")]
|
||||
#endif
|
||||
public static float ToAngle(this Vector2 self)
|
||||
{
|
||||
return Mathf.Atan2(self.y, self.x).Rad2Deg();
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Random", 7)]
|
||||
[APIDescriptionCN("针对随机做的一些封装")]
|
||||
[APIDescriptionEN("wrapper for random")]
|
||||
#endif
|
||||
public static class RandomUtility
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("随机选择")]
|
||||
[APIDescriptionEN("RandomChoose")]
|
||||
[APIExampleCode(@"
|
||||
var result = RandomUtility.Choose(1,1,1,2,2,2,2,3,3);
|
||||
|
||||
if (result == 3)
|
||||
{
|
||||
// todo ...
|
||||
}
|
||||
")]
|
||||
#endif
|
||||
public static T Choose<T>(params T[] args)
|
||||
{
|
||||
return args[UnityEngine.Random.Range(0, args.Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 095d03eec50f4f77828198360c9193d3
|
||||
timeCreated: 1647508997
|
||||
@@ -0,0 +1,313 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.Vector2/3", 8)]
|
||||
[APIDescriptionCN("针对 Vector2/Vector3 封装的函数")]
|
||||
[APIDescriptionEN("wrapper function for Vector2/Vector3")]
|
||||
[APIExampleCode(@"
|
||||
gameObjA.DirectionFrom(gameObjB);
|
||||
myComponentA.DirectionFrom(gameObjB);
|
||||
gameObjA.DirectionFrom(myComponentB);
|
||||
myComponentA.DirectionFrom(myComponentB);
|
||||
|
||||
// also support DirectionTo/ NormalizedDirectionFrom /NormalizedDirectionTo
|
||||
")]
|
||||
#endif
|
||||
public static class UnityEngineVectorExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("to.transform.position - self.transform.position")]
|
||||
[APIDescriptionEN("to.transform.position - self.transform.position")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.DirectionTo(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector3 DirectionTo(this Component self, Component to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector3 DirectionTo(this GameObject self, GameObject to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector3 DirectionTo(this Component self, GameObject to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector3 DirectionTo(this GameObject self, Component to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("self.transform.position - from.transform.position")]
|
||||
[APIDescriptionEN("self.transform.position - from.transform.position")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.DirectionFrom(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector3 DirectionFrom(this Component self, Component from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector3 DirectionFrom(this GameObject self, GameObject from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector3 DirectionFrom(this GameObject self, Component from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector3 DirectionFrom(this Component self, GameObject from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("(to.transform.position - self.transform.position).normalized")]
|
||||
[APIDescriptionEN("(to.transform.position - self.transform.position).normalized")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.NormalizedDirectionTo(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector3 NormalizedDirectionTo(this Component self, Component to) =>
|
||||
self.DirectionTo(to).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionTo(this GameObject self, GameObject to) =>
|
||||
self.DirectionTo(to).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionTo(this Component self, GameObject to) =>
|
||||
self.DirectionTo(to).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionTo(this GameObject self, Component to) =>
|
||||
self.DirectionTo(to).normalized;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("(self.transform.position - from.transform.position).normalized")]
|
||||
[APIDescriptionEN("(self.transform.position - from.transform.position).normalized")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.NormalizedDirectionTo(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector3 NormalizedDirectionFrom(this Component self, Component from) =>
|
||||
self.DirectionFrom(from).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionFrom(this GameObject self, GameObject from) =>
|
||||
self.DirectionFrom(from).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionFrom(this GameObject self, Component from) =>
|
||||
self.DirectionFrom(from).normalized;
|
||||
|
||||
public static Vector3 NormalizedDirectionFrom(this Component self, GameObject from) =>
|
||||
self.DirectionFrom(from).normalized;
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("(Vector2)(to.transform.position - self.transform.position)")]
|
||||
[APIDescriptionEN("(Vector2)(to.transform.position - self.transform.position)")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.Direction2DTo(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector2 Direction2DTo(this Component self, Component to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector2 Direction2DTo(this GameObject self, GameObject to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector2 Direction2DTo(this Component self, GameObject to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
public static Vector2 Direction2DTo(this GameObject self, Component to) =>
|
||||
to.transform.position - self.transform.position;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("(Vector2)(self.transform.position - from.transform.position)")]
|
||||
[APIDescriptionEN("(Vector2)(self.transform.position - from.transform.position)")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.Direction2DFrom(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector2 Direction2DFrom(this Component self, Component from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector2 Direction2DFrom(this GameObject self, GameObject from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector2 Direction2DFrom(this GameObject self, Component from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
public static Vector2 Direction2DFrom(this Component self, GameObject from) =>
|
||||
self.transform.position - from.transform.position;
|
||||
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("((Vector2)(to.transform.position - self.transform.position)).normalized")]
|
||||
[APIDescriptionEN("((Vector2)(to.transform.position - self.transform.position)).normalized")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.NormalizedDirection2DTo(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector2 NormalizedDirection2DTo(this Component self, Component to) =>
|
||||
self.Direction2DTo(to).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DTo(this GameObject self, GameObject to) =>
|
||||
self.Direction2DTo(to).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DTo(this Component self, GameObject to) =>
|
||||
self.Direction2DTo(to).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DTo(this GameObject self, Component to) =>
|
||||
self.Direction2DTo(to).normalized;
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.79
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("((Vector2)(self.transform.position - from.transform.position)).normalized")]
|
||||
[APIDescriptionEN("((Vector2)(self.transform.position - from.transform.position)).normalized")]
|
||||
[APIExampleCode(@"
|
||||
gameObj/otherComponent.NormalizedDirection2DFrom(otherGameObj/otherComponent)
|
||||
")]
|
||||
#endif
|
||||
public static Vector2 NormalizedDirection2DFrom(this Component self, Component from) =>
|
||||
self.Direction2DFrom(from).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DFrom(this GameObject self, GameObject from) =>
|
||||
self.Direction2DFrom(from).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DFrom(this GameObject self, Component from) =>
|
||||
self.Direction2DFrom(from).normalized;
|
||||
|
||||
public static Vector2 NormalizedDirection2DFrom(this Component self, GameObject from) =>
|
||||
self.Direction2DFrom(from).normalized;
|
||||
|
||||
|
||||
public static Vector2 ToVector2(this Vector3 self) => new Vector2(self.x, self.y);
|
||||
|
||||
public static Vector3 ToVector3(this Vector2 self, float z = 0)
|
||||
{
|
||||
return new Vector3(self.x, self.y, z);
|
||||
}
|
||||
|
||||
public static Vector3 X(this Vector3 self,float x)
|
||||
{
|
||||
self.x = x;
|
||||
return self;
|
||||
}
|
||||
|
||||
public static Vector3 Y(this Vector3 self,float y)
|
||||
{
|
||||
self.y = y;
|
||||
return self;
|
||||
}
|
||||
|
||||
public static Vector3 Z(this Vector3 self,float z)
|
||||
{
|
||||
self.z = z;
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
public static Vector2 X(this Vector2 self,float x)
|
||||
{
|
||||
self.x = x;
|
||||
return self;
|
||||
}
|
||||
|
||||
public static Vector2 Y(this Vector2 self,float y)
|
||||
{
|
||||
self.y = y;
|
||||
return self;
|
||||
}
|
||||
|
||||
public static float Distance(this GameObject self, GameObject other)
|
||||
{
|
||||
return Vector3.Distance(self.Position(), other.Position());
|
||||
}
|
||||
|
||||
public static float Distance(this Component self, GameObject other)
|
||||
{
|
||||
return Vector3.Distance(self.Position(), other.Position());
|
||||
}
|
||||
|
||||
public static float Distance(this GameObject self, Component other)
|
||||
{
|
||||
return Vector3.Distance(self.Position(), other.Position());
|
||||
}
|
||||
|
||||
public static float Distance(this Component self, Component other)
|
||||
{
|
||||
return Vector3.Distance(self.Position(), other.Position());
|
||||
}
|
||||
|
||||
public static float Distance2D(this GameObject self, GameObject other)
|
||||
{
|
||||
return Vector2.Distance(self.Position2D(), other.Position2D());
|
||||
}
|
||||
|
||||
public static float Distance2D(this Component self, GameObject other)
|
||||
{
|
||||
return Vector2.Distance(self.Position2D(), other.Position2D());
|
||||
}
|
||||
|
||||
public static float Distance2D(this GameObject self, Component other)
|
||||
{
|
||||
return Vector2.Distance(self.Position2D(), other.Position2D());
|
||||
}
|
||||
|
||||
public static float Distance2D(this Component self, Component other)
|
||||
{
|
||||
return Vector2.Distance(self.Position2D(), other.Position2D());
|
||||
}
|
||||
|
||||
public static float LocalDistance(this GameObject self, GameObject other)
|
||||
{
|
||||
return Vector3.Distance(self.LocalPosition(), other.LocalPosition());
|
||||
}
|
||||
|
||||
public static float LocalDistance(this Component self, GameObject other)
|
||||
{
|
||||
return Vector3.Distance(self.LocalPosition(), other.LocalPosition());
|
||||
}
|
||||
|
||||
public static float LocalDistance(this GameObject self, Component other)
|
||||
{
|
||||
return Vector3.Distance(self.LocalPosition(), other.LocalPosition());
|
||||
}
|
||||
|
||||
public static float LocalDistance(this Component self, Component other)
|
||||
{
|
||||
return Vector3.Distance(self.LocalPosition(), other.LocalPosition());
|
||||
}
|
||||
|
||||
public static float LocalDistance2D(this GameObject self, GameObject other)
|
||||
{
|
||||
return Vector2.Distance(self.LocalPosition2D(), other.LocalPosition2D());
|
||||
}
|
||||
|
||||
public static float LocalDistance2D(this Component self, GameObject other)
|
||||
{
|
||||
return Vector2.Distance(self.LocalPosition2D(), other.LocalPosition2D());
|
||||
}
|
||||
|
||||
public static float LocalDistance2D(this GameObject self, Component other)
|
||||
{
|
||||
return Vector2.Distance(self.LocalPosition2D(), other.LocalPosition2D());
|
||||
}
|
||||
|
||||
public static float LocalDistance2D(this Component self, Component other)
|
||||
{
|
||||
return Vector2.Distance(self.LocalPosition2D(), other.LocalPosition2D());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb2654f2feb44ad6b264c2d6a8c8a2de
|
||||
timeCreated: 1693400243
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("00.FluentAPI.Unity", "UnityEngine.RectTransform", 9)]
|
||||
[APIDescriptionCN("针对 RectTransform 封装的函数")]
|
||||
[APIDescriptionEN("wrapper function for RectTransform")]
|
||||
#endif
|
||||
public static class UnityEngineRectTransformExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.167
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("设置 rectTransform.anchoredPosition.y 值")]
|
||||
[APIDescriptionEN("set rectTransform.anchoredPosition.y value")]
|
||||
[APIExampleCode(@"
|
||||
text.rectTransform.AnchoredPositionY(5);
|
||||
")]
|
||||
#endif
|
||||
public static RectTransform AnchoredPositionY(this RectTransform selfRectTrans, float anchoredPositionY)
|
||||
{
|
||||
var anchorPos = selfRectTrans.anchoredPosition;
|
||||
anchorPos.y = anchoredPositionY;
|
||||
selfRectTrans.anchoredPosition = anchorPos;
|
||||
return selfRectTrans;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76b337d0ece04cab994eb2f50c6bd9aa
|
||||
timeCreated: 1733116027
|
||||
3
Assets/00.StaryEvo/Runtime/Tool/FluentAPI/1.CSharp.meta
Normal file
3
Assets/00.StaryEvo/Runtime/Tool/FluentAPI/1.CSharp.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 820be0af14a349f38700b599aa5252e3
|
||||
timeCreated: 1647422394
|
||||
@@ -0,0 +1,103 @@
|
||||
|
||||
using System;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Object", 0)]
|
||||
[APIDescriptionCN("针对 System.Object 提供的链式扩展,理论上任何对象都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.object can theoretically be used by any Object")]
|
||||
#endif
|
||||
public static class SystemObjectExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将自己传到 Action 委托中")]
|
||||
[APIDescriptionEN("apply self to the Action delegate")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject()
|
||||
.Self(gameObj=>gameObj.name = ""Enemy"")
|
||||
.Self(gameObj=>{
|
||||
Debug.Log(gameObj.name);
|
||||
});"
|
||||
)]
|
||||
#endif
|
||||
public static T Self<T>(this T self, Action<T> onDo)
|
||||
{
|
||||
onDo?.Invoke(self);
|
||||
return self;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.1.1
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("将自己传到 Func<T,T> 委托中,然后返回自己")]
|
||||
[APIDescriptionEN("apply self to the Func<T,T> delegate")]
|
||||
[APIExampleCode(@"
|
||||
new GameObject()
|
||||
.Self(gameObj=>gameObj.name = ""Enemy"")
|
||||
.Self(gameObj=>{
|
||||
Debug.Log(gameObj.name);
|
||||
});"
|
||||
)]
|
||||
#endif
|
||||
public static T Self<T>(this T self, Func<T,T> onDo)
|
||||
{
|
||||
return onDo.Invoke(self);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.2
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("判断是否为空")]
|
||||
[APIDescriptionEN("Check Is Null,return true or false")]
|
||||
[APIExampleCode(@"
|
||||
var simpleObject = new object();
|
||||
|
||||
if (simpleObject.IsNull()) // simpleObject == null
|
||||
{
|
||||
// do sth
|
||||
}")]
|
||||
#endif
|
||||
public static bool IsNull<T>(this T selfObj) where T : class
|
||||
{
|
||||
return null == selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.3
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("判断不是为空")]
|
||||
[APIDescriptionEN("Check Is Not Null,return true or false")]
|
||||
[APIExampleCode(@"
|
||||
var simpleObject = new object();
|
||||
|
||||
if (simpleObject.IsNotNull()) // simpleObject != null
|
||||
{
|
||||
// do sth
|
||||
}")]
|
||||
#endif
|
||||
public static bool IsNotNull<T>(this T selfObj) where T : class
|
||||
{
|
||||
return null != selfObj;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.36
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("转型")]
|
||||
[APIDescriptionEN("cast")]
|
||||
[APIExampleCode(@"
|
||||
int a = 10;
|
||||
Debug.Log(a.As<float>())
|
||||
// 10
|
||||
")]
|
||||
#endif
|
||||
public static T As<T>(this object selfObj) where T : class
|
||||
{
|
||||
return selfObj as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ef786a6b2b044b69fcc6abb9e695619
|
||||
timeCreated: 1647422404
|
||||
@@ -0,0 +1,275 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.String", 1)]
|
||||
[APIDescriptionCN("针对 System.String 提供的链式扩展,理论上任何集合都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.Collections can theoretically be used by any collection")]
|
||||
#endif
|
||||
public static class SystemStringExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.18
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检测是否为空或 Empty")]
|
||||
[APIDescriptionEN("Check Whether string is null or empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(string.Empty.IsNullOrEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsNullOrEmpty(this string selfStr)
|
||||
{
|
||||
return string.IsNullOrEmpty(selfStr);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.19
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检测是否为非空且非Empty")]
|
||||
[APIDescriptionEN("Checks both not null and non-empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""Hello"".IsNotNullAndEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsNotNullAndEmpty(this string selfStr)
|
||||
{
|
||||
return !string.IsNullOrEmpty(selfStr);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.20
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("去掉两端空格后,检测是否为空或 Empty")]
|
||||
[APIDescriptionEN("Check if it is Empty or Empty after removing whitespace from both sides")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log("" "".IsTrimNullOrEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsTrimNullOrEmpty(this string selfStr)
|
||||
{
|
||||
return selfStr == null || string.IsNullOrEmpty(selfStr.Trim());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Whether string trim is null or empty
|
||||
/// </summary>
|
||||
/// <param name="selfStr"></param>
|
||||
/// <returns></returns>
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.21
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("去掉两端空格后,检测是否为非 null 且非 Empty")]
|
||||
[APIDescriptionEN("After removing whitespace from both sides, check whether it is non-null and non-empty")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log("" 123 "".IsTrimNotNullAndEmpty());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool IsTrimNotNullAndEmpty(this string selfStr)
|
||||
{
|
||||
return selfStr != null && !string.IsNullOrEmpty(selfStr.Trim());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 缓存
|
||||
/// </summary>
|
||||
private static readonly char[] mCachedSplitCharArray = { '.' };
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.22
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串分割")]
|
||||
[APIDescriptionEN("String splitting")]
|
||||
[APIExampleCode(@"
|
||||
""1.2.3.4.5"".Split('.').ForEach(str=>Debug.Log(str));
|
||||
// 1 2 3 4 5
|
||||
")]
|
||||
#endif
|
||||
public static string[] Split(this string selfStr, char splitSymbol)
|
||||
{
|
||||
mCachedSplitCharArray[0] = splitSymbol;
|
||||
return selfStr.Split(mCachedSplitCharArray);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.23
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("格式化字符串填充参数")]
|
||||
[APIDescriptionEN("The format string populates the parameters")]
|
||||
[APIExampleCode(@"
|
||||
|
||||
var newStr = ""{0},{1}"".FillFormat(1,2);
|
||||
Debug.Log(newStr);
|
||||
// 1,2
|
||||
")]
|
||||
#endif
|
||||
public static string FillFormat(this string selfStr, params object[] args)
|
||||
{
|
||||
return string.Format(selfStr, args);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.24
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("返回包含此字符串的 StringBuilder")]
|
||||
[APIDescriptionEN("Returns a StringBuilder containing this string")]
|
||||
[APIExampleCode(@"
|
||||
var builder = ""Hello"".Builder();
|
||||
builder.Append("" QF"");
|
||||
Debug.Log(builder.ToString());
|
||||
// Hello QF
|
||||
")]
|
||||
#endif
|
||||
public static StringBuilder Builder(this string selfStr)
|
||||
{
|
||||
return new StringBuilder(selfStr);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.25
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("StringBuilder 添加前缀")]
|
||||
[APIDescriptionEN("StringBuilder insert prefix string")]
|
||||
[APIExampleCode(@"
|
||||
var builder = ""I'm liangxie"".Builder().AddPrefix(""Hi!"") ;
|
||||
Debug.Log(builder.ToString());
|
||||
// Hi!I'm liangxie
|
||||
")]
|
||||
#endif
|
||||
public static StringBuilder AddPrefix(this StringBuilder self, string prefixString)
|
||||
{
|
||||
self.Insert(0, prefixString);
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.26
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 Int")]
|
||||
[APIDescriptionEN("parse string to int")]
|
||||
[APIExampleCode(@"
|
||||
var number = ""123456"".ToInt();
|
||||
Debug.Log(number);
|
||||
// 123456
|
||||
// notice unsafe
|
||||
// 不安全
|
||||
")]
|
||||
#endif
|
||||
public static int ToInt(this string selfStr, int defaulValue = 0)
|
||||
{
|
||||
var retValue = defaulValue;
|
||||
return int.TryParse(selfStr, out retValue) ? retValue : defaulValue;
|
||||
}
|
||||
|
||||
public static long ToLong(this string self, long defaultValue = 0)
|
||||
{
|
||||
var retValue = defaultValue;
|
||||
return long.TryParse(self, out retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.27
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 Int")]
|
||||
[APIDescriptionEN("parse string to int")]
|
||||
[APIExampleCode(@"
|
||||
DateTime.Now.ToString().ToDataTime();
|
||||
")]
|
||||
#endif
|
||||
public static DateTime ToDateTime(this string selfStr, DateTime defaultValue = default(DateTime))
|
||||
{
|
||||
return DateTime.TryParse(selfStr, out var retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.28
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字符串解析成 float")]
|
||||
[APIDescriptionEN("parse string to float")]
|
||||
[APIExampleCode(@"
|
||||
var number = ""123456f"".ToInt();
|
||||
Debug.Log(number);
|
||||
// 123456
|
||||
// notice unsafe
|
||||
// 不安全
|
||||
")]
|
||||
#endif
|
||||
public static float ToFloat(this string selfStr, float defaultValue = 0)
|
||||
{
|
||||
return float.TryParse(selfStr, out var retValue) ? retValue : defaultValue;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.29
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("是否存在中文字符")]
|
||||
[APIDescriptionEN("check string contains chinese or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""你好"".HasChinese());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool HasChinese(this string input)
|
||||
{
|
||||
return Regex.IsMatch(input, @"[\u4e00-\u9fa5]");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.30
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("是否存在空格")]
|
||||
[APIDescriptionEN("check string contains space or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""你好 "".HasSpace());
|
||||
// true
|
||||
")]
|
||||
#endif
|
||||
public static bool HasSpace(this string input)
|
||||
{
|
||||
return input.Contains(" ");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.31
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("remove string")]
|
||||
[APIDescriptionEN("check string contains space or not")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(""Hello World "".RemoveString(""Hello"","" ""));
|
||||
// World
|
||||
")]
|
||||
#endif
|
||||
public static string RemoveString(this string str, params string[] targets)
|
||||
{
|
||||
return targets.Aggregate(str, (current, t) => current.Replace(t, string.Empty));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1.0.39
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("join string")]
|
||||
[APIDescriptionEN("join string")]
|
||||
[APIExampleCode(@"
|
||||
Debug.Log(new List<string>() { ""1"",""2"",""3""}.StringJoin("",""));
|
||||
// 1,2,3
|
||||
")]
|
||||
#endif
|
||||
public static string StringJoin(this IEnumerable<string> self, string separator)
|
||||
{
|
||||
return string.Join(separator, self);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16f9643181cd400085f8615855718a05
|
||||
timeCreated: 1647445231
|
||||
@@ -0,0 +1,183 @@
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.IO", 2)]
|
||||
[APIDescriptionCN("针对 System.IO 提供的链式扩展,主要是文件和文件夹的一些 IO 操作")]
|
||||
[APIDescriptionEN("IO chain extension for system. IO, mainly file and folder IO operations")]
|
||||
#endif
|
||||
public static class SystemIOExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.10
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("创建文件夹,如果存在则不创建")]
|
||||
[APIDescriptionEN("Create folder or not if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir = ""Assets/TestFolder"";
|
||||
testDir.CreateDirIfNotExists();"
|
||||
)]
|
||||
#endif
|
||||
public static string CreateDirIfNotExists(this string dirFullPath)
|
||||
{
|
||||
if (!Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.CreateDirectory(dirFullPath);
|
||||
}
|
||||
|
||||
return dirFullPath;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.11
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("删除文件夹,如果存在")]
|
||||
[APIDescriptionEN("Delete the folder if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir =""Assets/TestFolder"";
|
||||
testDir.DeleteDirIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static void DeleteDirIfExists(this string dirFullPath)
|
||||
{
|
||||
if (Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.Delete(dirFullPath, true);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.12
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("清空 Dir(保留目录),如果存在")]
|
||||
[APIDescriptionEN("Clear Dir (reserved directory), if exists")]
|
||||
[APIExampleCode(@"
|
||||
var testDir = ""Assets/TestFolder"";
|
||||
testDir.EmptyDirIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static void EmptyDirIfExists(this string dirFullPath)
|
||||
{
|
||||
if (Directory.Exists(dirFullPath))
|
||||
{
|
||||
Directory.Delete(dirFullPath, true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(dirFullPath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.13
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("删除文件 如果存在")]
|
||||
[APIDescriptionEN("Delete the file if it exists")]
|
||||
[APIExampleCode(@"
|
||||
var filePath = ""Assets/Test.txt"";
|
||||
File.Create(""Assets/Test"");
|
||||
filePath.DeleteFileIfExists();
|
||||
")]
|
||||
#endif
|
||||
public static bool DeleteFileIfExists(this string fileFullPath)
|
||||
{
|
||||
if (File.Exists(fileFullPath))
|
||||
{
|
||||
File.Delete(fileFullPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.14
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("合并路径")]
|
||||
[APIDescriptionEN("Combine path")]
|
||||
[APIExampleCode(@"
|
||||
var path = Application.dataPath.CombinePath(""Resources"");
|
||||
Debug.Log(Path)
|
||||
// projectPath/Assets/Resources
|
||||
")]
|
||||
#endif
|
||||
public static string CombinePath(this string selfPath, string toCombinePath)
|
||||
{
|
||||
return Path.Combine(selfPath, toCombinePath);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.15
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件名")]
|
||||
[APIDescriptionEN("get file name by path")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileName();
|
||||
Debug.Log(fileName0);
|
||||
// b.txt
|
||||
")]
|
||||
#endif
|
||||
public static string GetFileName(this string filePath)
|
||||
{
|
||||
return Path.GetFileName(filePath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.16
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件名,不包含文件扩展名")]
|
||||
[APIDescriptionEN("Get the file name based on the path, excluding the file name extension")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileNameWithoutExtend();
|
||||
Debug.Log(fileName0);
|
||||
// b
|
||||
")]
|
||||
#endif
|
||||
|
||||
public static string GetFileNameWithoutExtend(this string filePath)
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(filePath);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.17
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("根据路径获取文件扩展名")]
|
||||
[APIDescriptionEN("Get the file extension based on the path")]
|
||||
[APIExampleCode(@"
|
||||
var fileName =""/abc/def/b.txt"".GetFileExtendName();
|
||||
Debug.Log(fileName0);
|
||||
// .txt
|
||||
")]
|
||||
#endif
|
||||
public static string GetFileExtendName(this string filePath)
|
||||
{
|
||||
return Path.GetExtension(filePath);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.156
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取文件夹路径")]
|
||||
[APIDescriptionEN("get filePath's folder path")]
|
||||
[APIExampleCode(@"
|
||||
var folderPath =""/abc/def/b.txt"".GetFolderPath();
|
||||
Debug.Log(fileName0);
|
||||
// /abs/def
|
||||
")]
|
||||
#endif
|
||||
public static string GetFolderPath(this string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return Path.GetDirectoryName(path);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 214fbbb4fd6f426f8e9867263025a775
|
||||
timeCreated: 1647438572
|
||||
@@ -0,0 +1,188 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Collections", 3)]
|
||||
[APIDescriptionCN("针对 System.Collections 提供的链式扩展,理论上任何集合都可以使用")]
|
||||
[APIDescriptionEN("The chain extension provided by System.Collections can theoretically be used by any collection")]
|
||||
#endif
|
||||
public static class CollectionsExtension
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.4
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历 IEnumerable")]
|
||||
[APIDescriptionEN("ForEach for IEnumerable")]
|
||||
[APIExampleCode(@"
|
||||
IEnumerable<int> testIEnumerable = new List<int> { 1, 2, 3 };
|
||||
testIEnumerable.ForEach(number => Debug.Log(number));
|
||||
// output
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
new Dictionary<string, string>()
|
||||
{
|
||||
{""name"",""liangxie""},
|
||||
{""company"",""liangxiegame"" }
|
||||
}
|
||||
.ForEach(keyValue => Debug.LogFormat(""key:{0},value:{1}"", keyValue.Key, keyValue.Value));
|
||||
// key:name,value:liangxie
|
||||
// key:company,value:liangxiegame")]
|
||||
#endif
|
||||
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> self, Action<T> action)
|
||||
{
|
||||
foreach (var item in self)
|
||||
{
|
||||
action(item);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.5
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("List 倒序遍历")]
|
||||
[APIDescriptionEN("Reverse ForEach for List")]
|
||||
[APIExampleCode(@"
|
||||
var testList = new List<int> { 1, 2, 3 };
|
||||
testList.ForEachReverse(number => number.LogInfo());
|
||||
// 3 2 1
|
||||
")]
|
||||
#endif
|
||||
public static List<T> ForEachReverse<T>(this List<T> selfList, Action<T> action)
|
||||
{
|
||||
for (var i = selfList.Count - 1; i >= 0; --i)
|
||||
action(selfList[i]);
|
||||
|
||||
return selfList;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.6
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历 List (可获得索引)")]
|
||||
[APIDescriptionEN("foreach List (can get index)")]
|
||||
[APIExampleCode(@"
|
||||
var testList = new List<string> {""a"", ""b"", ""c"" };
|
||||
testList.Foreach((c,index)=>Debug.Log(index));
|
||||
// 1, 2, 3,
|
||||
")]
|
||||
#endif
|
||||
public static void ForEach<T>(this List<T> list, Action<int, T> action)
|
||||
{
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
action(i, list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.7
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("遍历字典")]
|
||||
[APIDescriptionEN("ForEach Dictionary")]
|
||||
[APIExampleCode(@"
|
||||
var infos = new Dictionary<string,string> {{""name"",""liangxie""},{""age"",""18""}};
|
||||
infos.ForEach((key,value)=> Debug.LogFormat(""{0}:{1}"",key,value);
|
||||
// name:liangxie
|
||||
// age:18
|
||||
")]
|
||||
#endif
|
||||
public static void ForEach<K, V>(this Dictionary<K, V> dict, Action<K, V> action)
|
||||
{
|
||||
var dictE = dict.GetEnumerator();
|
||||
|
||||
while (dictE.MoveNext())
|
||||
{
|
||||
var current = dictE.Current;
|
||||
action(current.Key, current.Value);
|
||||
}
|
||||
|
||||
dictE.Dispose();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.8
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("合并字典")]
|
||||
[APIDescriptionEN("Merge Dictionaries")]
|
||||
[APIExampleCode(@"
|
||||
var dictionary1 = new Dictionary<string, string> { { ""1"", ""2"" } };
|
||||
var dictionary2 = new Dictionary<string, string> { { ""3"", ""4"" } };
|
||||
var dictionary3 = dictionary1.Merge(dictionary2);
|
||||
dictionary3.ForEach(pair => Debug.LogFormat(""{0}:{1}"", pair.Key, pair.Value));
|
||||
// 1:2
|
||||
// 3:4
|
||||
|
||||
// notice: duplicate keys are not supported.
|
||||
// 注意:不支持重复的 key。
|
||||
")]
|
||||
#endif
|
||||
public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
|
||||
params Dictionary<TKey, TValue>[] dictionaries)
|
||||
{
|
||||
return dictionaries.Aggregate(dictionary,
|
||||
(current, dict) => current.Union(dict).ToDictionary(kv => kv.Key, kv => kv.Value));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.9
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("字典添加新的字典")]
|
||||
[APIDescriptionEN("Dictionary Adds a new dictionary")]
|
||||
[APIExampleCode(@"
|
||||
var dictionary1 = new Dictionary<string, string> { { ""1"", ""2"" } };
|
||||
var dictionary2 = new Dictionary<string, string> { { ""1"", ""4"" } };
|
||||
var dictionary3 = dictionary1.AddRange(dictionary2,true); // true means override
|
||||
dictionary3.ForEach(pair => Debug.LogFormat(""{0}:{1}"", pair.Key, pair.Value));
|
||||
// 1:2
|
||||
// 3:4
|
||||
|
||||
// notice: duplicate keys are supported.
|
||||
// 注意:支持重复的 key。
|
||||
")]
|
||||
#endif
|
||||
public static void AddRange<K, V>(this Dictionary<K, V> dict, Dictionary<K, V> addInDict,
|
||||
bool isOverride = false)
|
||||
{
|
||||
var enumerator = addInDict.GetEnumerator();
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
var current = enumerator.Current;
|
||||
if (dict.ContainsKey(current.Key))
|
||||
{
|
||||
if (isOverride)
|
||||
dict[current.Key] = current.Value;
|
||||
continue;
|
||||
}
|
||||
|
||||
dict.Add(current.Key, current.Value);
|
||||
}
|
||||
|
||||
enumerator.Dispose();
|
||||
}
|
||||
|
||||
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this T[] collection) => collection == null || collection.Length == 0;
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this IList<T> collection) => collection == null || collection.Count == 0;
|
||||
// TODO:
|
||||
public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection) => collection == null || !collection.Any();
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this T[] collection) => !IsNullOrEmpty(collection);
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this IList<T> collection) => !IsNullOrEmpty(collection);
|
||||
// TODO:
|
||||
public static bool IsNotNullAndEmpty<T>(this IEnumerable<T> collection) => !IsNullOrEmpty(collection);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1be94df70e04fbf880352766fa84326
|
||||
timeCreated: 1647436071
|
||||
@@ -0,0 +1,300 @@
|
||||
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[ClassAPI("01.FluentAPI.CSharp", "System.Reflection", 4)]
|
||||
[APIDescriptionCN("针对 System.Reflection 提供的链式扩展")]
|
||||
[APIDescriptionEN("Chain extension provided for System.Reflection")]
|
||||
#endif
|
||||
public static class SystemReflectionExtension
|
||||
{
|
||||
// [UnityEditor.MenuItem("QF/Test")]
|
||||
// public static void Test()
|
||||
// {
|
||||
// "/abc/e.txt".GetFileExtendName().LogInfo();
|
||||
// }
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过 Type 创建 Instance")]
|
||||
[APIDescriptionEN("Create Instance By Type")]
|
||||
[APIExampleCode(@"
|
||||
|
||||
interface IA
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
}
|
||||
|
||||
IA a = typeof(A).CreateInstance<IA>();
|
||||
|
||||
")]
|
||||
#endif
|
||||
public static T CreateInstance<T>(this Type self) where T : class
|
||||
{
|
||||
// 获取构造函数
|
||||
var constructorInfos = self.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
// 获取无参构造函数
|
||||
var ctor = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
|
||||
|
||||
return ctor.Invoke(null) as T;
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.32
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过反射的方式调用私有方法")]
|
||||
[APIDescriptionEN("call private method by reflection")]
|
||||
[APIExampleCode(@"
|
||||
class A
|
||||
{
|
||||
private void Say() { Debug.Log(""I'm A!"") }
|
||||
}
|
||||
|
||||
new A().ReflectionCallPrivateMethod(""Say"");
|
||||
// I'm A!
|
||||
")]
|
||||
#endif
|
||||
public static object ReflectionCallPrivateMethod<T>(this T self, string methodName, params object[] args)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var methodInfo = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
return methodInfo?.Invoke(self, args);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.33
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("通过反射的方式调用私有方法,有返回值")]
|
||||
[APIDescriptionEN("call private method by reflection,return the result")]
|
||||
[APIExampleCode(@"
|
||||
class A
|
||||
{
|
||||
private bool Add(int a,int b) { return a + b; }
|
||||
}
|
||||
|
||||
Debug.Log(new A().ReflectionCallPrivateMethod(""Add"",1,2));
|
||||
// 3
|
||||
")]
|
||||
#endif
|
||||
public static TReturnType ReflectionCallPrivateMethod<T, TReturnType>(this T self, string methodName,
|
||||
params object[] args)
|
||||
{
|
||||
return (TReturnType)self.ReflectionCallPrivateMethod(methodName, args);
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.34
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
[APIExampleCode(@"
|
||||
[DisplayName(""A Class"")
|
||||
class A
|
||||
{
|
||||
[DisplayName(""A Number"")
|
||||
public int Number;
|
||||
|
||||
[DisplayName(""Is Complete?"")
|
||||
private bool Complete => Number > 100;
|
||||
|
||||
[DisplayName(""Say complete result?"")
|
||||
public void SayComplete()
|
||||
{
|
||||
Debug.Log(Complete);
|
||||
}
|
||||
}
|
||||
|
||||
var aType = typeof(A);
|
||||
//
|
||||
Debug.Log(aType.HasAttribute(typeof(DisplayNameAttribute));
|
||||
// true
|
||||
Debug.Log(aType.HasAttribute<DisplayNameAttribute>());
|
||||
// true
|
||||
|
||||
// also support MethodInfo、PropertyInfo、FieldInfo
|
||||
// 同时 也支持 MethodInfo、PropertyInfo、FieldInfo
|
||||
")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this Type type, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return type.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this Type type, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return type.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this PropertyInfo prop, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return prop.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this PropertyInfo prop, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return prop.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this FieldInfo field, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return field.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this FieldInfo field, Type attributeType, bool inherit)
|
||||
{
|
||||
return field.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute<T>(this MethodInfo method, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return method.GetCustomAttributes(typeof(T), inherit).Any();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("检查是否有指定的 Attribute")]
|
||||
[APIDescriptionEN("Check whether the specified Attribute exists")]
|
||||
#endif
|
||||
public static bool HasAttribute(this MethodInfo method, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return method.GetCustomAttributes(attributeType, inherit).Any();
|
||||
}
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// v1 No.35
|
||||
[MethodAPI]
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
[APIExampleCode(@"
|
||||
[DisplayName(""A Class"")
|
||||
class A
|
||||
{
|
||||
[DisplayName(""A Number"")
|
||||
public int Number;
|
||||
|
||||
[DisplayName(""Is Complete?"")
|
||||
private bool Complete => Number > 100;
|
||||
|
||||
[DisplayName(""Say complete result?"")
|
||||
public void SayComplete()
|
||||
{
|
||||
Debug.Log(Complete);
|
||||
}
|
||||
}
|
||||
|
||||
var aType = typeof(A);
|
||||
//
|
||||
Debug.Log(aType.GetAttribute(typeof(DisplayNameAttribute));
|
||||
// DisplayNameAttribute
|
||||
Debug.Log(aType.GetAttribute<DisplayNameAttribute>());
|
||||
// DisplayNameAttribute
|
||||
|
||||
// also support MethodInfo、PropertyInfo、FieldInfo
|
||||
// 同时 也支持 MethodInfo、PropertyInfo、FieldInfo
|
||||
")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this Type type, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return type.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this Type type, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return type.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this MethodInfo method, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return method.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this MethodInfo method, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return method.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this FieldInfo field, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return field.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this FieldInfo field, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return field.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static T GetAttribute<T>(this PropertyInfo prop, bool inherit = false) where T : Attribute
|
||||
{
|
||||
return prop.GetCustomAttributes<T>(inherit).FirstOrDefault();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[APIDescriptionCN("获取指定的 Attribute")]
|
||||
[APIDescriptionEN("Gets the specified Attribute")]
|
||||
#endif
|
||||
public static object GetAttribute(this PropertyInfo prop, Type attributeType, bool inherit = false)
|
||||
{
|
||||
return prop.GetCustomAttributes(attributeType, inherit).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ad1fb17a7ff4016be06cd22911d23f9
|
||||
timeCreated: 1647484400
|
||||
Reference in New Issue
Block a user