private GameObject GetInstance() { Action <GameObject, Material> setMaterialFunc = (go, material) => { MeshRenderer[] renderers = go.GetComponentsInChildren <MeshRenderer>(); foreach (var renderer in renderers) { renderer.sharedMaterial = material; } }; // Moving create parent m_segments from Initialize to here because the // editor will delete it when the user presses play then stop then "Undo". if (m_segments == null) { m_segments = RuntimeObjects.GetOrCreateRoot(m_parentComponent); if (m_segments == null) { return(null); } if (m_segments.transform.childCount > 0) { if (m_separateFirstObjectPrefabPath != string.Empty) { m_firstSegmentInstance = m_segments.transform.GetChild(0).gameObject; } if (m_firstSegmentInstance != null && m_segments.transform.childCount > 1) { m_segmentInstance = m_segments.transform.GetChild(1).gameObject; } else if (m_firstSegmentInstance == null) { m_segmentInstance = m_segments.transform.GetChild(0).gameObject; } } } if (m_separateFirstObjectPrefabPath != string.Empty && m_firstSegmentInstance == null) { m_firstSegmentInstance = PrefabLoader.Instantiate <GameObject>(m_separateFirstObjectPrefabPath); m_firstSegmentInstance.hideFlags = HideFlags.DontSaveInEditor; setMaterialFunc(m_firstSegmentInstance, Material); AddSelectionProxy(m_firstSegmentInstance); Add(m_firstSegmentInstance); } else if (m_segmentInstance == null) { m_segmentInstance = PrefabLoader.Instantiate <GameObject>(m_prefabObjectPath); m_segmentInstance.hideFlags = HideFlags.DontSaveInEditor; setMaterialFunc(m_segmentInstance, Material); AddSelectionProxy(m_segmentInstance); Add(m_segmentInstance); } // Push back new segment if there aren't enough segments already created. int currentChildCount = m_segments.transform.childCount; if (m_counter == currentChildCount) { Add(GameObject.Instantiate(m_segmentInstance)); } else if (m_counter > currentChildCount) { throw new Exception("Internal counter is not in sync. Counter = " + m_counter + ", #children = " + currentChildCount); } return(m_segments.transform.GetChild(m_counter++).gameObject); }
private void Update() { OnTrackReset(); if (Tracks.Length == 0) { m_uninitializedTrackData.Clear(); return; } if (m_root == null) { m_root = RuntimeObjects.GetOrCreateRoot(this); } var containsNullEntries = m_uninitializedTrackData.FirstOrDefault(pair => pair.Key == null).Value != null; if (containsNullEntries) { m_uninitializedTrackData = m_uninitializedTrackData.Where(pair => pair.Key != null).ToDictionary(pair => pair.Key, pair => pair.Value); } var numNodes = 0; foreach (var track in Tracks) { if (!track.isActiveAndEnabled) { continue; } if (track.Native != null) { numNodes += (int)track.Native.getNumNodes(); } else { if (!m_uninitializedTrackData.ContainsKey(track)) { m_uninitializedTrackData.Add(track, new UninitializedTrackData()); } var trackData = m_uninitializedTrackData[track]; trackData.Update(track); numNodes += trackData.TrackNodes.Length; } } if (numNodes > m_root.transform.childCount) { var numToAdd = numNodes - m_root.transform.childCount; // If we're rendering several tracks it doesn't matter (I think) // which of them that receives the OnSelectionProxy. var refTrack = Tracks[0]; for (int i = 0; i < numToAdd; ++i) { var instance = Instantiate(Resource); Configure(refTrack, instance); m_root.AddChild(instance); } } else if (numNodes < m_root.transform.childCount) { var numToRemove = m_root.transform.childCount - numNodes; for (int i = 0; i < numToRemove; ++i) { DestroyImmediate(m_root.transform.GetChild(m_root.transform.childCount - 1).gameObject); } } var nodeCounter = 0; foreach (var track in Tracks) { if (!track.isActiveAndEnabled) { continue; } if (track.Native != null) { foreach (var node in track.Native.nodes()) { var renderInstance = m_root.transform.GetChild(nodeCounter++); renderInstance.rotation = node.getRigidBody().getRotation().ToHandedQuaternion(); renderInstance.position = node.getBeginPosition().ToHandedVector3() + renderInstance.TransformDirection(0.5f * (float)node.getLength() * Vector3.forward); if (AutomaticScaling) { renderInstance.localScale = 2.0f * node.getHalfExtents().ToVector3(); } else { renderInstance.localScale = new Vector3(1, 1, 1); } } } else { var nodes = m_uninitializedTrackData[track].TrackNodes; foreach (var node in nodes) { var renderInstance = m_root.transform.GetChild(nodeCounter++); renderInstance.rotation = node.Rotation; renderInstance.position = node.Position + renderInstance.TransformDirection(node.HalfExtents.z * Vector3.forward); if (AutomaticScaling) { renderInstance.localScale = 2.0f * node.HalfExtents; } else { renderInstance.localScale = new Vector3(1, 1, 1); //2.0f * node.HalfExtents; } } } } }