Created
May 17, 2022 23:45
-
-
Save mfdeveloper/60f215db697f83f5fd10b0915b2c19f7 to your computer and use it in GitHub Desktop.
Unity: C# string extension to allow use string templates in Unity Editor inspector, like: {gameObject.name}, {transform.parent.name} ...
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
using System.Collections.Generic; | |
using UnityEngine; | |
namespace AudioGame.UnityExtensions | |
{ | |
public static class StringExtensions | |
{ | |
public static Dictionary<string, string> BuildTemplateTokens(GameObject gameObject) | |
{ | |
var templateTokens = new Dictionary<string, string>() | |
{ | |
{ "gameObject.name", gameObject.name }, | |
{ "gameObject.tag", gameObject.tag } | |
}; | |
// Scene templates | |
if (!string.IsNullOrEmpty(gameObject.scene.name) && gameObject.scene.IsValid()) | |
{ | |
templateTokens.Add("gameObject.scene.name", gameObject.scene.name); | |
} | |
// Transform parent templates | |
if (gameObject.transform.parent != null) | |
{ | |
templateTokens.Add("transform.parent.name", gameObject.transform.parent.name); | |
} | |
return templateTokens; | |
} | |
public static Dictionary<string, string> BuildTemplateTokens(Component component) | |
{ | |
return BuildTemplateTokens(component.gameObject); | |
} | |
public static bool IsTemplate(this string str) | |
{ | |
return str.StartsWith("{") && str.EndsWith("}"); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <remarks> | |
/// <a href="https://stackoverflow.com/questions/3773857/escape-curly-brace-in-string-format">Escape curly brace '{' in String.Format</a> | |
/// </remarks> | |
/// <param name="str"></param> | |
/// <param name="gameObject"></param> | |
/// <returns></returns> | |
public static string ReplaceTemplateOf(this string str, GameObject gameObject) | |
{ | |
var templateTokens = BuildTemplateTokens(gameObject); | |
if (IsTemplate(str)) | |
{ | |
string resultStr = (string) str.Clone(); | |
foreach (var token in templateTokens) | |
{ | |
resultStr = resultStr.Replace(string.Format("{{{0}}}", token.Key), token.Value); | |
} | |
return resultStr; | |
} | |
return str; | |
} | |
public static string ReplaceTemplateOf(this string str, Component component) | |
{ | |
return ReplaceTemplateOf(str, component.gameObject); | |
} | |
} | |
} |
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
using System.Collections; | |
using System.Collections.Generic; | |
using NUnit.Framework; | |
using UnityEngine; | |
using UnityEngine.TestTools; | |
using AudioGame.UnityExtensions; | |
public class StringExtensionsTest | |
{ | |
class MyExampleScript : MonoBehaviour | |
{ | |
} | |
// A Test behaves as an ordinary method | |
[Test] | |
public void TestIfStringWithTokenIsReplaced() | |
{ | |
string strTemplate = "{gameObject.name}"; | |
string gameObjName = "myGameObject"; | |
var myExampleComponent = new GameObject(gameObjName).AddComponent<MyExampleScript>(); | |
Assert.AreEqual(gameObjName, strTemplate.ReplaceTemplateOf(myExampleComponent)); | |
} | |
[Test] | |
public void TestGetTheOriginalStringWhenDontHaveTemplateToken() | |
{ | |
string strTemplate = "{gameObject.test}"; | |
string gameObjName = "myGameObject"; | |
var myExampleComponent = new GameObject(gameObjName).AddComponent<MyExampleScript>(); | |
Assert.AreNotEqual(gameObjName, strTemplate.ReplaceTemplateOf(myExampleComponent)); | |
Assert.AreEqual(strTemplate, strTemplate.ReplaceTemplateOf(myExampleComponent)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment