Skip to content

Commit 81304e7

Browse files
authored
fix: add empty transforms to the list of objects for making instances (#903)
* Ensure parent objects are added when an object is created in a hierarchy with empty transforms. * Fix for compiler error in yamato * Refactoring to make the code more complicated * Using existing method instead of new one
1 parent 1e47e88 commit 81304e7

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

Runtime/Scripts/BaseMeshSync.cs

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,13 +1746,98 @@ private EntityRecord UpdatePointsEntity(PointsData data, MeshSyncPlayerConfig co
17461746
return rec;
17471747
}
17481748

1749+
static Transform FindOrCreateByPath(Transform parent, string path, Action<string> parentCreationCallback, bool worldPositionStays = true) {
1750+
string[] names = path.Split('/');
1751+
if (names.Length <= 0)
1752+
return null;
1753+
1754+
//if parent is null, search from root
1755+
Transform t = parent;
1756+
int tokenStartIdx = 0;
1757+
if (null == t) {
1758+
string rootGameObjectName = names[0];
1759+
t = FilmInternalUtilities.GameObjectUtility.FindFirstRoot(rootGameObjectName);
1760+
if (null == t) {
1761+
GameObject go = new GameObject(rootGameObjectName);
1762+
t = go.GetComponent<Transform>();
1763+
}
1764+
1765+
tokenStartIdx = 1;
1766+
}
1767+
1768+
static Transform FindOrCreateChild(Transform t, string childName, out bool didCreate, bool worldPositionStays = true) {
1769+
Transform childT = t.Find(childName);
1770+
if (null != childT) {
1771+
didCreate = false;
1772+
return childT;
1773+
}
1774+
1775+
GameObject go = new GameObject(childName);
1776+
childT = go.transform;
1777+
childT.SetParent(t, worldPositionStays);
1778+
didCreate = true;
1779+
return childT;
1780+
}
1781+
1782+
//loop over hierarchy of names and generate parents that don't exist
1783+
int nameLength = names.Length;
1784+
List<string> processedParents = new List<string>();
1785+
for (int i = tokenStartIdx; i < nameLength; ++i) {
1786+
string nameToken = names[i];
1787+
if (string.IsNullOrEmpty(nameToken))
1788+
continue;
1789+
1790+
processedParents.Add(nameToken);
1791+
1792+
t = FindOrCreateChild(t, nameToken, out var didCreate, worldPositionStays);
1793+
if (i < nameLength - 1 && didCreate) {
1794+
var parentPath = String.Join("/", processedParents);
1795+
if (!parentPath.StartsWith("/")) {
1796+
parentPath = $"/{parentPath}";
1797+
}
1798+
1799+
parentCreationCallback?.Invoke(parentPath);
1800+
}
1801+
1802+
if (null == t)
1803+
return null;
1804+
}
1805+
1806+
return t;
1807+
}
1808+
1809+
void AddClientObject(string path, out EntityRecord rec) {
1810+
if (m_clientObjects.TryGetValue(path, out rec))
1811+
if (rec.go == null) {
1812+
m_clientObjects.Remove(path);
1813+
rec = null;
1814+
}
1815+
1816+
if (rec == null) {
1817+
var trans = FindOrCreateByPath(m_rootObject, path,
1818+
delegate(string parentPath) {
1819+
EntityRecord parentRec = null;
1820+
AddClientObject(parentPath, out parentRec);
1821+
if (parentRec.dataType == EntityType.Unknown)
1822+
parentRec.dataType = EntityType.Transform;
1823+
},
1824+
false);
1825+
1826+
rec = new EntityRecord {
1827+
go = trans.gameObject,
1828+
trans = trans,
1829+
recved = true
1830+
};
1831+
m_clientObjects.Add(path, rec);
1832+
}
1833+
}
1834+
17491835
private EntityRecord UpdateTransformEntity(TransformData data, MeshSyncPlayerConfig config) {
17501836
string path = data.path;
17511837
int hostID = data.hostID;
17521838
if (path.Length == 0)
17531839
return null;
17541840

1755-
Transform trans = null;
17561841
EntityRecord rec = null;
17571842
if (hostID != Lib.invalidID) {
17581843
if (m_hostObjects.TryGetValue(hostID, out rec))
@@ -1765,21 +1850,7 @@ private EntityRecord UpdateTransformEntity(TransformData data, MeshSyncPlayerCon
17651850
return null;
17661851
}
17671852
else {
1768-
if (m_clientObjects.TryGetValue(path, out rec))
1769-
if (rec.go == null) {
1770-
m_clientObjects.Remove(path);
1771-
rec = null;
1772-
}
1773-
1774-
if (rec == null) {
1775-
trans = FilmInternalUtilities.GameObjectUtility.FindOrCreateByPath(m_rootObject, path, false);
1776-
rec = new EntityRecord {
1777-
go = trans.gameObject,
1778-
trans = trans,
1779-
recved = true
1780-
};
1781-
m_clientObjects.Add(path, rec);
1782-
}
1853+
AddClientObject(path, out rec);
17831854
}
17841855

17851856
return UpdateTransformEntity(data, config, rec);

0 commit comments

Comments
 (0)