From adf4922b2283b84285bf350ba2d07eff8f23e580 Mon Sep 17 00:00:00 2001 From: Kornei Dontsov Date: Wed, 18 Jan 2023 16:34:10 +0300 Subject: [PATCH] [Unity3D-sdk] Fix conflicts between different UnityNodes This commit allows folders 'fairygui', 'ngui', 'ugui' and 'uguiWithTMPro' to exists in project simultaneously without any compile errors as long as they have all the required dependencies. User can choose preferred UnityNode to use manually or it may be resolved automatically. While it's difficult to imagine the situation where an end user want to keep multiple UnityNode scripts simultaneously, this improvement helps to develop sdk without deleting/restoring folders, and also, in perspective, separate code into different assemblies and packages for Unity 2019 and newer. Note: TextMeshPro related code was set under define UNITY_2018_1_OR_NEWER, because TextMeshPro supports Unity 2018.1 and newer. Keeping this code for older versions is bad, because it makes hard to test sdk for older versions of Unity without removing the folder. So this change actually changes nothing, only makes life easier. --- Unity3D/PocoManager.cs | 31 ++++++++++++++++-- Unity3D/UnityDumper.cs | 21 +++++++----- Unity3D/UnityNodeProvider.cs | 10 ++++++ .../fairygui/FairyGuiUnityNodeProvider.asset | Bin 0 -> 4192 bytes Unity3D/fairygui/FairyGuiUnityNodeProvider.cs | 17 ++++++++++ Unity3D/fairygui/UnityNode.cs | 2 +- Unity3D/ngui/NGuiUnityNodeProvider.asset | Bin 0 -> 4188 bytes Unity3D/ngui/NGuiUnityNodeProvider.cs | 17 ++++++++++ Unity3D/ngui/UnityNode.cs | 2 +- Unity3D/ugui/UGuiUnityNodeProvider.asset | Bin 0 -> 4188 bytes Unity3D/ugui/UGuiUnityNodeProvider.cs | 17 ++++++++++ Unity3D/ugui/UnityNode.cs | 2 +- .../UGuiWithTMProUnityNodeProvider.asset | Bin 0 -> 4196 bytes .../UGuiWithTMProUnityNodeProvider.cs | 17 ++++++++++ Unity3D/uguiWithTMPro/UnityNode.cs | 9 ++--- 15 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 Unity3D/UnityNodeProvider.cs create mode 100644 Unity3D/fairygui/FairyGuiUnityNodeProvider.asset create mode 100644 Unity3D/fairygui/FairyGuiUnityNodeProvider.cs create mode 100644 Unity3D/ngui/NGuiUnityNodeProvider.asset create mode 100644 Unity3D/ngui/NGuiUnityNodeProvider.cs create mode 100644 Unity3D/ugui/UGuiUnityNodeProvider.asset create mode 100644 Unity3D/ugui/UGuiUnityNodeProvider.cs create mode 100644 Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.asset create mode 100644 Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.cs diff --git a/Unity3D/PocoManager.cs b/Unity3D/PocoManager.cs index c2c46f8..d62937f 100644 --- a/Unity3D/PocoManager.cs +++ b/Unity3D/PocoManager.cs @@ -15,12 +15,13 @@ public class PocoManager : MonoBehaviour { public const int versionCode = 6; + [SerializeField] private UnityNodeProvider nodeProvider; public int port = 5001; private bool mRunning; public AsyncTcpServer server = null; private RPCParser rpc = null; private SimpleProtocolFilter prot = null; - private UnityDumper dumper = new UnityDumper(); + private UnityDumper dumper = null; private ConcurrentDictionary inbox = new ConcurrentDictionary(); private VRSupport vr_support = new VRSupport(); private Dictionary debugProfilingData = new Dictionary() { @@ -35,10 +36,36 @@ class RPC : Attribute { } +#if UNITY_EDITOR + private void OnValidate() + { + if (!nodeProvider) + { + UnityNodeProvider otherNodeProvider = null; + foreach (var nodeFactoryAssetGuid in UnityEditor.AssetDatabase.FindAssets("t:UnityNodeProvider")) + { + var nodeFactoryAssetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(nodeFactoryAssetGuid); + otherNodeProvider = UnityEditor.AssetDatabase.LoadAssetAtPath(nodeFactoryAssetPath); + if (otherNodeProvider) break; + } + + if (otherNodeProvider) + { + nodeProvider = otherNodeProvider; + } + else + { + Debug.LogError("Failed to find UnityNodeProvider for Poco."); + } + } + } +#endif + void Awake() { Application.runInBackground = true; DontDestroyOnLoad(this); + dumper = new UnityDumper(nodeProvider); prot = new SimpleProtocolFilter(); rpc = new RPCParser(); rpc.addRpcMethod("isVRSupported", vr_support.isVRSupported); @@ -170,7 +197,7 @@ private object SetText(List param) { if (go.GetInstanceID() == instanceId) { - return UnityNode.SetText(go, textVal); + return nodeProvider.SetText(go, textVal); } } return false; diff --git a/Unity3D/UnityDumper.cs b/Unity3D/UnityDumper.cs index d2afe1c..188912a 100644 --- a/Unity3D/UnityDumper.cs +++ b/Unity3D/UnityDumper.cs @@ -1,29 +1,34 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; namespace Poco { public class UnityDumper : AbstractDumper { + private readonly UnityNodeProvider nodeProvider; + + public UnityDumper(UnityNodeProvider nodeProvider) + { + this.nodeProvider = nodeProvider; + } + public override AbstractNode getRoot() { - return new RootNode(); + return nodeProvider ? new RootNode(nodeProvider) : null; } } public class RootNode : AbstractNode { - private List children = null; + private readonly List children = new List(); - public RootNode() + public RootNode(UnityNodeProvider nodeProvider) { - children = new List(); - foreach (GameObject obj in Transform.FindObjectsOfType(typeof(GameObject))) + foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject))) { if (obj.transform.parent == null) { - children.Add(new UnityNode(obj)); + children.Add(nodeProvider.CreateNode(obj)); } } } diff --git a/Unity3D/UnityNodeProvider.cs b/Unity3D/UnityNodeProvider.cs new file mode 100644 index 0000000..44c64ec --- /dev/null +++ b/Unity3D/UnityNodeProvider.cs @@ -0,0 +1,10 @@ +using UnityEngine; + +namespace Poco +{ + public abstract class UnityNodeProvider: ScriptableObject + { + public abstract AbstractNode CreateNode(GameObject gameObject); + public abstract bool SetText(GameObject go, string textVal); + } +} \ No newline at end of file diff --git a/Unity3D/fairygui/FairyGuiUnityNodeProvider.asset b/Unity3D/fairygui/FairyGuiUnityNodeProvider.asset new file mode 100644 index 0000000000000000000000000000000000000000..db609be8d0f520835b75efa610d7ce4059fb331f GIT binary patch literal 4192 zcmeH~zb__19LB%%zTA5phx6m^C^QPyiy(>l>&?B+qzW}Ek?v%B zvTMFWo{GhyQUIunm%AHdt&eS2kNHGp+W7yYbaHgd+*y12HGSDRbKi3I4SB5y<|mZ6 zJt8jWp^Fx|l7qOorx+^%VxAxTiu?nwKpjIcSD|%-nGNVOKOW&p=rp7MF&Ojnm8{3a zJr8tBBl%IC4-p>KNrYTU7j*K04%ZlXXFp{T9@QxixspBX^EY&O!cPb_;Gc~?jDquA zJoAeW3t0CqSho_K<*g5&J?8W1y12biX9W6((4qglQssL2ssny9$ZN<`DQB|$Y1O8KHt;-8gRpK?B1b_<>c(4KaQDQ zv2xiyS)LiTmdu}nXc%+O0vgmnNj)6-4SZ)2Jep17ufdkw7LL8zo6_HZGQW4R|93lm zu{zm!y~+Pd7wv#{Ks%rv&<gfY>L3_IGB8{Pit_+zCLji>WdySB0dXFT1LlQ?ehaE1?$Y26ArwnQi$PPy=?%_ldM>dBGF3!lX0O-zGpgABPasx4= zBgAi@aPS6-gZu^ZGb|i3fcCzE+RFI$hD#*HLo}`KhKUKH{LBXC)Lvh z$OuR*$pA7yW&nZ1=d}<9kOU=Skk^VpA`m-3EQSY_>%lZw%a@90t3qs#D9rHP66`+h zttBX3je^k-7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu;scnCndTi|XLtUm?n;`zCk aW`^cvmQ?!Xr=$iH<(Flqq=NfjKsf-~GJ`n) literal 0 HcmV?d00001 diff --git a/Unity3D/ngui/NGuiUnityNodeProvider.cs b/Unity3D/ngui/NGuiUnityNodeProvider.cs new file mode 100644 index 0000000..df65bb1 --- /dev/null +++ b/Unity3D/ngui/NGuiUnityNodeProvider.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Poco.NGUI +{ + public class NGuiUnityNodeProvider : UnityNodeProvider + { + public override AbstractNode CreateNode(GameObject gameObject) + { + return new UnityNode(gameObject); + } + + public override bool SetText(GameObject go, string textVal) + { + return UnityNode.SetText(go, textVal); + } + } +} \ No newline at end of file diff --git a/Unity3D/ngui/UnityNode.cs b/Unity3D/ngui/UnityNode.cs index 9edc484..aa2e3e9 100644 --- a/Unity3D/ngui/UnityNode.cs +++ b/Unity3D/ngui/UnityNode.cs @@ -4,7 +4,7 @@ using UnityEngine; -namespace Poco +namespace Poco.NGUI { public class UnityNode : AbstractNode { diff --git a/Unity3D/ugui/UGuiUnityNodeProvider.asset b/Unity3D/ugui/UGuiUnityNodeProvider.asset new file mode 100644 index 0000000000000000000000000000000000000000..0395b34ac9922c5417ed5a69784d7f184a7c24f8 GIT binary patch literal 4188 zcmZQzVEW9!AP~dAz`zfrffNISsh)wJNtz+UzyJULGcp1Nfb@UXbrJWrUwt3Pns%gq z>4A_k)*$cx)Opqa!ow~a$G=h8B>_~Y0mLBn?2HTz<_ruCAbmhCkOdNFWLU<`(69+8 z#{v`rse@n;$-rn}2ATs3e^8V% zHh|SL8Un);ghAqhAaMtfI5Rk$rGa`t?hyisgE2EhAW&@<5QF%_aB)Tkkom??^F=`7 z4In*CDCUcT#2rB5sOE#>6Px+sQ1O6(k|G<|l+2R+BG-zN)V$)%{5(5`+<3RloK#O2 zAR{2LBm>9*nE?b2pVvYdKoXRQL0&5Yi9qZCu^1jyt_Ra#E%)k-XNv4?slNSWfBBK8 zY$>30H3~*UU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!n;2{9*Zh^a1u>KUNix=u% ani-mxSyJhjpOP9-lwX#ak_zsB0p$QUtb|Vh literal 0 HcmV?d00001 diff --git a/Unity3D/ugui/UGuiUnityNodeProvider.cs b/Unity3D/ugui/UGuiUnityNodeProvider.cs new file mode 100644 index 0000000..5489b7f --- /dev/null +++ b/Unity3D/ugui/UGuiUnityNodeProvider.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Poco.UGUI +{ + public class UGuiUnityNodeProvider : UnityNodeProvider + { + public override AbstractNode CreateNode(GameObject gameObject) + { + return new UnityNode(gameObject); + } + + public override bool SetText(GameObject go, string textVal) + { + return UnityNode.SetText(go, textVal); + } + } +} \ No newline at end of file diff --git a/Unity3D/ugui/UnityNode.cs b/Unity3D/ugui/UnityNode.cs index c9ba1b6..dab314c 100644 --- a/Unity3D/ugui/UnityNode.cs +++ b/Unity3D/ugui/UnityNode.cs @@ -5,7 +5,7 @@ using TMPro; -namespace Poco +namespace Poco.UGUI { public class UnityNode : AbstractNode { diff --git a/Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.asset b/Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.asset new file mode 100644 index 0000000000000000000000000000000000000000..48e83853661f44dbadca9a17bdc0da063f90f210 GIT binary patch literal 4196 zcmeH~y-OTH7{=e(i}5>t#ux(HSfn}>l>|xT#P3LC{kT|(2)lYKdI#r5lr&q2mgxi= zu?m7nz)lMbv9n5%Hl(o;I}5uzpJ#VnR`E}mfp_Pf-@fzA%(L5!h;b`YYl%n=dUPV) z$@XN|LWdNJ#iCLG=!=WyW%tQ;?Z^FeMd6^zYAcx>`)Tj}D9n8O(>Zt3cG`@(_XyUP zD)Byuc$|kJGIAxq65<`>-3uVr1;MAtU+^mQ27eAzpmqttaYMwFP`@+OZzT6P$9Sawnmk4RSfAhXIsG>wH%&MHY0z@qTz+uf zwX>^EF54?Bb3@LuJvxBIoOd%wsDYNcJqi*$Ed-C2CkfVICjU2n`@KJ9@=L$k-PyeB zF|N+eKfj&gf2EstKs%rv&<CkR!Q1rkcW1_N aGt*zqd@h}J+>P;^Wg}m6EZe_`iQEHbl!Jo+ literal 0 HcmV?d00001 diff --git a/Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.cs b/Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.cs new file mode 100644 index 0000000..07834bf --- /dev/null +++ b/Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Poco.UGUIWithTMPro +{ + public class UGuiWithTMProUnityNodeProvider : UnityNodeProvider + { + public override AbstractNode CreateNode(GameObject gameObject) + { + return new UnityNode(gameObject); + } + + public override bool SetText(GameObject go, string textVal) + { + return UnityNode.SetText(go, textVal); + } + } +} \ No newline at end of file diff --git a/Unity3D/uguiWithTMPro/UnityNode.cs b/Unity3D/uguiWithTMPro/UnityNode.cs index 94b9652..9305b7a 100644 --- a/Unity3D/uguiWithTMPro/UnityNode.cs +++ b/Unity3D/uguiWithTMPro/UnityNode.cs @@ -2,10 +2,9 @@ using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; -using TMPro; -namespace Poco +namespace Poco.UGUIWithTMPro { public class UnityNode : AbstractNode { @@ -215,16 +214,18 @@ private bool GameObjectClickable(List components) private string GameObjectText() { - TMP_Text tmpText = gameObject.GetComponent(); +#if UNITY_2018_1_OR_NEWER + var tmpText = gameObject.GetComponent(); if (tmpText) { return tmpText.GetParsedText(); } - TextMeshProUGUI tmpUIText = gameObject.GetComponent(); + var tmpUIText = gameObject.GetComponent(); if (tmpUIText) { return tmpUIText.GetParsedText(); } +#endif Text text = gameObject.GetComponent(); return text ? text.text : null; }