/// <summary> /// Destroy this gameObject. THIS IS NOT NETWORKED SYNCED. /// </summary> /// <param name="gameObject"></param> public static void Destroy(GameObject gameObject) { //prevent destroy from being called on a gameobject multiple times enqueing if (gameObject._markedForDestruction) return; gameObject._markedForDestruction = true; GameState.DestroyDelays += () => { DestroyNow(gameObject); }; }
/// <summary> /// run when a gameobject is added to the room /// </summary> /// <param name="gameObject"></param> public virtual void OnGameObjectAdded(GameObject gameObject){}
public void CoroutineTest() { var gobj = new GameObject(); var test = gobj.AddComponent<TestCoroutine>(); test.StartCoroutine(test.DoThing()); //one extra Run is required as it's assumed that this would be the same frame that the coroutine would be started in. test.RunCoroutines(); Assert.AreEqual(test.OuterCount, 1); Assert.AreEqual(test.InnerCount, 0); test.RunCoroutines(); Assert.AreEqual(test.OuterCount, 1); Assert.AreEqual(test.InnerCount, 1); test.RunCoroutines(); Assert.AreEqual(test.OuterCount, 1); Assert.AreEqual(test.InnerCount, 2); test.RunCoroutines(); Assert.AreEqual(test.OuterCount, 2); Assert.AreEqual(test.InnerCount, 2); test.StartCoroutine(test.MoreEnumerator()); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 0); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 1); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 2); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 3); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 4); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 5); test.RunCoroutines(); Assert.AreEqual(test.MoreEnumCount, 5); }
public void TestWaitFrames() { var gobj = new GameObject(); var test = gobj.AddComponent<TestCoroutine>(); test.StartCoroutine(test.FrameWaiter()); Assert.IsFalse(test.HasWaitedForFrames); //one extra Run is required as it's assumed that this would be the same frame that the coroutine would be started in. test.RunCoroutines(); Assert.IsFalse(test.HasWaitedForFrames); //now these are real frames. test.RunCoroutines(); Assert.IsFalse(test.HasWaitedForFrames); test.RunCoroutines(); Assert.IsFalse(test.HasWaitedForFrames); test.RunCoroutines(); Assert.IsTrue(test.HasWaitedForFrames); }
/// <summary> /// Load a gameobject from a file /// </summary> /// <param name="filePath"></param> /// <param name="roomToInstantiateIn"></param> /// <param name="position"> </param> /// <param name="rotation"> </param> /// <param name="allowNetworkInstantiateIfHasNetworkView"></param> /// <param name="visibleToAll">makes all players in the room subscribed to the object</param> /// <param name="owner">owner of the loaded object if network instantiated. By default, it is the server</param> /// <returns></returns> public static GameObject Load(string filePath, Room roomToInstantiateIn, bool allowNetworkInstantiateIfHasNetworkView = false, Vector3? position = null, Quaternion? rotation = null, bool visibleToAll = true, Player owner = null) { var dser = new GameObject(); dser.Room = roomToInstantiateIn; var awakes = new List<Action>(); var config = new YamlConfig(); var actualFilePath = Path.Combine(ResourceFolder, filePath + ".prefab"); config.AddActivator<GameObject>(() => { //be returning an object we've already created, the AddComponent will work properly return dser; }); //config.AddActivator < List<ComponentTracker>>(() => //{ // return dser.components; //}); var trackers = new Stack<GameObject.ComponentTracker>(); config.AddActivator<GameObject.ComponentTracker>(() => { var ntrack = new GameObject.ComponentTracker(); trackers.Push(ntrack); return ntrack; }); foreach (Type t in GetComponentTypes()) { Type tLocal = t; if (tLocal == typeof(NetworkView) && !allowNetworkInstantiateIfHasNetworkView) { Debug.LogWarning("[Resources.Load] file {0} has a NetworkView component on it, but was run as to not network instantiate. It will not be networked.", actualFilePath); } GameObject dser1 = dser; config.AddActivator(tLocal, () => { Action awake; var ntrack = trackers.Pop(); var ret = dser1.DeserializeAddComponent(tLocal, out awake, ntrack); awakes.Add(awake); return ret; }); } var serializer = new YamlSerializer(config); serializer.DeserializeFromFile(actualFilePath, typeof(GameObject)); if (dser.Resource == null) dser.Resource = filePath; if (position.HasValue) dser.Position = position.Value; if (rotation.HasValue) dser.Rotation = rotation.Value; roomToInstantiateIn.OnGameObjectAdded(dser); foreach (var awake in awakes) if (awake != null) awake(); dser.OnComponentAfterDeserialization(); if (allowNetworkInstantiateIfHasNetworkView && dser.GetComponent<NetworkView>() != null) roomToInstantiateIn.ResourceNetworkInstantiate(dser, visibleToAll, owner); return dser; }
internal static int AddGameObject(GameObject newObject) { var newId = GameObjects.Add(newObject); return newId; }
internal static void RemoveObject(GameObject gobj) { GameObjects.Remove(gobj.Id); }
internal static GameObject CreateGameObject(SlimMath.Vector3 position, SlimMath.Quaternion rotation) { var gameObject = new GameObject {Position = position, Rotation = rotation}; return gameObject; }
internal static void DestroyNow(GameObject gameObject) { gameObject._markedForDestruction = true; //just in case gameObject.OnDestroy(); gameObject.components.ForEach(g => g.component.Dispose()); gameObject.components = null; GameState.RemoveObject(gameObject); }
internal static void RemoveObject(GameObject gobj) { // if (gobj.Id == -1) { Debug.LogError("Attempted to remove a gameobject with an Id of -1. This happens only if the gameobject has not fully registered with the gamestate before being destroyed. This should never happen. Throwing."); throw new Exception("Attempted to remove a gameobject with an Id of -1. This would be due to a gameobject not fully registering itself with the gamestate before being destroyed. This should never happen"); } GameObjects.Remove(gobj.Id); }
internal static int AddGameObject(GameObject newObject) { var nInd = objects.Count; objects.Add(newObject); return nInd; }
/// <summary> /// Destroy this gameObject. THIS IS NOT NETWORKED SYNCED. /// </summary> /// <param name="gameObject"></param> public static void Destroy(GameObject gameObject) { gameObject.OnDestroy(); gameObject.components.ForEach(g => g.component.Dispose()); gameObject.components = null; GameState.RemoveObject(gameObject); }
void SendNetworkInstantiate(List<NetConnection> connections, GameObject gobj) { var netView = gobj.GetComponent<NetworkView>(); if (netView == null) { Debug.Log("[Instantiate] the specified object {0} does not have a network view to actually use for network instantiation", gobj.Resource); } var message = PNetServer.peer.CreateMessage(33 + (gobj.Resource.Length * 2)); message.Write(RPCUtils.Instantiate); message.Write(gobj.Resource); message.Write(netView.viewID.guid); message.Write(netView.owner.Id); var vs = new Vector3Serializer(gobj.Position); vs.OnSerialize(message); var qs = new QuaternionSerializer(gobj.Rotation); qs.OnSerialize(message); if (connections.Count > 0) PNetServer.peer.SendMessage(message, connections , NetDeliveryMethod.ReliableOrdered, Channels.STATIC_UTILS); }
/// <summary> /// Create a static game object that can receive rpc's in the scene. /// The client should have a similar object in the scene matching the room with the same id /// </summary> /// <param name="sceneObject"></param> /// <param name="gobj"></param> public void NetworkedRoomObject(NetworkedSceneObject sceneObject, GameObject gobj) { if (gobj.Room != this && gobj.Room != null) { Debug.LogError("cannot move the object to the room. it must have no room or be in this one."); return; } var sceneView = gobj.AddComponent<NetworkedSceneObjectView>(); gobj.Room = this; sceneView.room = this; sceneView.NetworkID = sceneObject.NetworkID; roomObjects[sceneObject.NetworkID] = sceneView; //sceneObject.OnFinishedCreation(); }