Skip to content

Commit

Permalink
[Unity3D-sdk] Fix conflicts between different UnityNodes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KorneiDontsov committed Jan 18, 2023
1 parent 4a9e388 commit adf4922
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 17 deletions.
31 changes: 29 additions & 2 deletions Unity3D/PocoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TcpClientState> inbox = new ConcurrentDictionary<string, TcpClientState>();
private VRSupport vr_support = new VRSupport();
private Dictionary<string, long> debugProfilingData = new Dictionary<string, long>() {
Expand All @@ -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<UnityNodeProvider>(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);
Expand Down Expand Up @@ -170,7 +197,7 @@ private object SetText(List<object> param)
{
if (go.GetInstanceID() == instanceId)
{
return UnityNode.SetText(go, textVal);
return nodeProvider.SetText(go, textVal);
}
}
return false;
Expand Down
21 changes: 13 additions & 8 deletions Unity3D/UnityDumper.cs
Original file line number Diff line number Diff line change
@@ -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<AbstractNode> children = null;
private readonly List<AbstractNode> children = new List<AbstractNode>();

public RootNode()
public RootNode(UnityNodeProvider nodeProvider)
{
children = new List<AbstractNode>();
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));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions Unity3D/UnityNodeProvider.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Binary file added Unity3D/fairygui/FairyGuiUnityNodeProvider.asset
Binary file not shown.
17 changes: 17 additions & 0 deletions Unity3D/fairygui/FairyGuiUnityNodeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine;

namespace Poco.FairyGUI
{
public class FairyGuiUnityNodeProvider : UnityNodeProvider
{
public override AbstractNode CreateNode(GameObject gameObject)
{
return new UnityNode(gameObject);
}

public override bool SetText(GameObject go, string textVal)
{
return UnityNode.SetText(go, textVal);
}
}
}
2 changes: 1 addition & 1 deletion Unity3D/fairygui/UnityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using FairyGUI;

namespace Poco
namespace Poco.FairyGUI
{
public class UnityNode : AbstractNode
{
Expand Down
Binary file added Unity3D/ngui/NGuiUnityNodeProvider.asset
Binary file not shown.
17 changes: 17 additions & 0 deletions Unity3D/ngui/NGuiUnityNodeProvider.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Unity3D/ngui/UnityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine;


namespace Poco
namespace Poco.NGUI
{
public class UnityNode : AbstractNode
{
Expand Down
Binary file added Unity3D/ugui/UGuiUnityNodeProvider.asset
Binary file not shown.
17 changes: 17 additions & 0 deletions Unity3D/ugui/UGuiUnityNodeProvider.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Unity3D/ugui/UnityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using TMPro;


namespace Poco
namespace Poco.UGUI
{
public class UnityNode : AbstractNode
{
Expand Down
Binary file not shown.
17 changes: 17 additions & 0 deletions Unity3D/uguiWithTMPro/UGuiWithTMProUnityNodeProvider.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
9 changes: 5 additions & 4 deletions Unity3D/uguiWithTMPro/UnityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using TMPro;


namespace Poco
namespace Poco.UGUIWithTMPro
{
public class UnityNode : AbstractNode
{
Expand Down Expand Up @@ -215,16 +214,18 @@ private bool GameObjectClickable(List<string> components)

private string GameObjectText()
{
TMP_Text tmpText = gameObject.GetComponent<TMP_Text>();
#if UNITY_2018_1_OR_NEWER
var tmpText = gameObject.GetComponent<TMPro.TMP_Text>();
if (tmpText)
{
return tmpText.GetParsedText();
}
TextMeshProUGUI tmpUIText = gameObject.GetComponent<TextMeshProUGUI>();
var tmpUIText = gameObject.GetComponent<TMPro.TextMeshProUGUI>();
if (tmpUIText)
{
return tmpUIText.GetParsedText();
}
#endif
Text text = gameObject.GetComponent<Text>();
return text ? text.text : null;
}
Expand Down

0 comments on commit adf4922

Please sign in to comment.