-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. InputField 增加回车提交功能(OnEnterSubmit) 2. GetOrAddComponent 支持 Component 对象
- Loading branch information
1 parent
98fad36
commit db507f8
Showing
11 changed files
with
298 additions
and
171 deletions.
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
QFramework/QFramework.Unity.Runtime/Core/CSharpExtension/GameObjectExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/**************************************************************************** | ||
* Copyright (c) 2021.4 liangxie | ||
* | ||
* https://qframework.cn | ||
* https://github.com/liangxiegame/QFramework | ||
* https://gitee.com/liangxiegame/QFramework | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
****************************************************************************/ | ||
|
||
using System; | ||
using UnityEngine; | ||
|
||
namespace QFramework | ||
{ | ||
/// <summary> | ||
/// GameObject's Util/Static This Extension | ||
/// </summary> | ||
public static class GameObjectExtension | ||
{ | ||
public static void Example() | ||
{ | ||
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"); | ||
} | ||
|
||
#region CEGO001 Show | ||
|
||
public static GameObject Show(this GameObject selfObj) | ||
{ | ||
selfObj.SetActive(true); | ||
return selfObj; | ||
} | ||
|
||
public static T Show<T>(this T selfComponent) where T : Component | ||
{ | ||
selfComponent.gameObject.Show(); | ||
return selfComponent; | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO002 Hide | ||
|
||
public static GameObject Hide(this GameObject selfObj) | ||
{ | ||
selfObj.SetActive(false); | ||
return selfObj; | ||
} | ||
|
||
public static T Hide<T>(this T selfComponent) where T : Component | ||
{ | ||
selfComponent.gameObject.Hide(); | ||
return selfComponent; | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO003 DestroyGameObj | ||
|
||
public static void DestroyGameObj<T>(this T selfBehaviour) where T : Component | ||
{ | ||
selfBehaviour.gameObject.DestroySelf(); | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO004 DestroyGameObjGracefully | ||
|
||
public static void DestroyGameObjGracefully<T>(this T selfBehaviour) where T : Component | ||
{ | ||
if (selfBehaviour && selfBehaviour.gameObject) | ||
{ | ||
selfBehaviour.gameObject.DestroySelfGracefully(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO005 DestroyGameObjGracefully | ||
|
||
public static T DestroyGameObjAfterDelay<T>(this T selfBehaviour, float delay) where T : Component | ||
{ | ||
selfBehaviour.gameObject.DestroySelfAfterDelay(delay); | ||
return selfBehaviour; | ||
} | ||
|
||
public static T DestroyGameObjAfterDelayGracefully<T>(this T selfBehaviour, float delay) where T : Component | ||
{ | ||
if (selfBehaviour && selfBehaviour.gameObject) | ||
{ | ||
selfBehaviour.gameObject.DestroySelfAfterDelay(delay); | ||
} | ||
|
||
return selfBehaviour; | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO006 Layer | ||
|
||
public static GameObject Layer(this GameObject selfObj, int layer) | ||
{ | ||
selfObj.layer = layer; | ||
return selfObj; | ||
} | ||
|
||
public static T Layer<T>(this T selfComponent, int layer) where T : Component | ||
{ | ||
selfComponent.gameObject.layer = layer; | ||
return selfComponent; | ||
} | ||
|
||
public static GameObject Layer(this GameObject selfObj, string layerName) | ||
{ | ||
selfObj.layer = LayerMask.NameToLayer(layerName); | ||
return selfObj; | ||
} | ||
|
||
public static T Layer<T>(this T selfComponent, string layerName) where T : Component | ||
{ | ||
selfComponent.gameObject.layer = LayerMask.NameToLayer(layerName); | ||
return selfComponent; | ||
} | ||
|
||
#endregion | ||
|
||
#region CEGO007 Component | ||
|
||
public static T GetOrAddComponent<T>(this GameObject selfComponent) where T : Component | ||
{ | ||
var comp = selfComponent.gameObject.GetComponent<T>(); | ||
return comp ? comp : selfComponent.gameObject.AddComponent<T>(); | ||
} | ||
|
||
public static T GetOrAddComponent<T>(this Component component) where T : Component | ||
{ | ||
return component.gameObject.GetOrAddComponent<T>(); | ||
} | ||
|
||
public static Component GetOrAddComponent(this GameObject selfComponent, Type type) | ||
{ | ||
var comp = selfComponent.gameObject.GetComponent(type); | ||
return comp ? comp : selfComponent.gameObject.AddComponent(type); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.