示例#1
0
        /// <summary>
        /// Generate a prefab based in object colider layer
        /// </summary>
        /// <param name="obj">Object which properties will be used to generate a prefab.</param>
        /// <param name="newColliderObject">if null add relative parent object,.</param>
        /// <returns>Generated Game Object containing the Collider.</returns>
        public void AddPrefabs(MapObject obj, GameObject newColliderObject = null, bool is2DColliders = false, bool addTileName = true)
        {
            int indexPrefab = 0;
            while ("".Equals(obj.GetPropertyAsString(indexPrefab + "-prefab")) == false)
            {
                string prefabName = obj.GetPropertyAsString(indexPrefab + "-prefab");
                string baseResourcePath = obj.GetPropertyAsString(indexPrefab + "-prefab path");
                UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName);
                Resources.UnloadUnusedAssets();
                if (resourceObject != null)
                {
                    float zDepth = obj.GetPropertyAsFloat(indexPrefab + "-prefab z depth");
                    GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject;
                    //if (newColliderObject == null)
                    //{
                    //	newPrefab.transform.parent = MapObject.transform;
                    //	newPrefab.transform.localPosition = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, zDepth);
                    //}
                    //else
                    //{
                    //	newPrefab.transform.parent = newColliderObject.transform;
                    //	newPrefab.transform.localPosition = new Vector3(0, 0, zDepth);
                    //}
                    newPrefab.transform.parent = MapObject.transform;
                    newPrefab.transform.localPosition = new Vector3(obj.Bounds.center.x, -obj.Bounds.center.y, zDepth);

                    // copy coliders from newColliderObject
                    // only copy if type of this object is diferent of "NoCollider"
                    if ("NoCollider".Equals(obj.Type) == false)
                    {
                        if (obj.GetPropertyAsBoolean(indexPrefab + "-prefab add collider"))
                        {
                            CopyCollider(obj, ref newColliderObject, ref newPrefab, is2DColliders);
                        }
                    }

                    newPrefab.name = (addTileName ? (_mapName + "_") : "") + obj.Name;
                    int indexMessage = 0;
                    string prefabMensage = obj.GetPropertyAsString(indexPrefab + "-prefab sendmessage " + indexMessage);
                    while ("".Equals(prefabMensage) == false)
                    {
                        string[] menssage = prefabMensage.Split(new[] { '|' }, StringSplitOptions.None);
                        if (menssage.Length == 2)
                        {
                            newPrefab.BroadcastMessage(menssage[0], menssage[1]);
                        }
                        indexMessage++;
                        prefabMensage = obj.GetPropertyAsString(indexPrefab + "-prefab sendmessage " + indexMessage);
                    }

                }
                else
                {
                    Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName);
                }
                indexPrefab++;
            }
        }
        /// <summary>
        /// Generate a prefab based in object layer
        /// </summary>
        /// <param name="obj">Object which properties will be used to generate a prefab.</param>
        /// <param name="parent">if null add relative parent object,.</param>
        /// <param name="addMapName">true to add Map's name to the prefab name</param>
        /// <returns>Generated GameObject from the Prefab.</returns>
        public static GameObject GeneratePrefab(this Map map, MapObject obj, Vector2 anchorPointValue, GameObject parent = null, bool addMapName = true, bool setNameAsObjectName = false)
        {
            if (obj.HasProperty(Map.Property_PrefabName))
            {
                string prefabName = obj.GetPropertyAsString(Map.Property_PrefabName);
                string baseResourcePath = obj.GetPropertyAsString(Map.Property_PrefabPath);
                UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName);
                Resources.UnloadUnusedAssets();
                if (resourceObject != null)
                {
                    float zDepth = obj.GetPropertyAsFloat(Map.Property_PrefabZDepth);
                    GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject;

                    newPrefab.transform.parent = obj.ParentObjectLayer != null ? obj.ParentObjectLayer.LayerGameObject.transform : map.MapGameObject.transform;
                    newPrefab.transform.localPosition = map.TiledPositionToWorldPoint(
                        new Vector3(obj.Bounds.xMin + obj.Bounds.width * anchorPointValue.x,
                            obj.Bounds.yMin + obj.Bounds.height * anchorPointValue.y,
                            zDepth));

                    if (obj.HasProperty(Map.Property_PrefabAddCollider))
                    {
                        string colliderType = obj.GetPropertyAsString(Map.Property_PrefabAddCollider);
                        AddCollider(map, newPrefab, obj, !colliderType.Contains("3"));
                    }

                    // since custom properties are automatically added only when a collider is added, we must enforce them to be parsed
                    ApplyCustomProperties(newPrefab, obj);

                    if (parent)
                        newPrefab.transform.parent = parent.transform;

                    if (setNameAsObjectName)
                        newPrefab.name = obj.Name;

                    if (addMapName)
                        newPrefab.name = string.Concat(map.MapName, "_", newPrefab.name);

                    obj.LinkedGameObject = newPrefab;

                    return newPrefab;
                }
                else
                {
                    Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName);
                }
            }
            return null;
        }
示例#3
0
        /// <summary>
        /// Generate a prefab based in object colider layer
        /// </summary>
        /// <param name="obj">Object which properties will be used to generate a prefab.</param>
        /// <param name="newColliderObject">if null add relative parent object,.</param>
        /// <param name="addTileName">true to add Map's name to the prefab name</param>
        /// <param name="is2DColliders">true to generate 2D colliders</param>
        /// <returns>Generated Game Object containing the Collider.</returns>
        public void AddPrefabs(MapObject obj, GameObject newColliderObject = null, bool is2DColliders = false, bool addTileName = true)
        {
            int indexPrefab = 0;
            while (obj.HasProperty(string.Concat(indexPrefab.ToString(), Property_PrefabName)))
            {
                string prefabName = obj.GetPropertyAsString(indexPrefab + Property_PrefabName);
                string baseResourcePath = obj.GetPropertyAsString(indexPrefab + Property_PrefabPath);
                UnityEngine.Object resourceObject = Resources.Load(baseResourcePath + prefabName);
                Resources.UnloadUnusedAssets();
                if (resourceObject != null)
                {
                    float zDepth = obj.GetPropertyAsFloat(indexPrefab + Property_PrefabZDepth);
                    GameObject newPrefab = UnityEngine.Object.Instantiate(resourceObject) as GameObject;

                    newPrefab.transform.parent = obj.ParentObjectLayer != null ? obj.ParentObjectLayer.LayerGameObject.transform : MapObject.transform;
                    newPrefab.transform.localPosition = TiledPositionToWorldPoint(new Vector3(obj.Bounds.center.x, obj.Bounds.center.y, zDepth));

                    // copy coliders from newColliderObject
                    // only copy if type of this object is diferent of "NoCollider"
                    if (obj.Type.Equals(Object_Type_NoCollider) == false)
                    {
                        if (obj.GetPropertyAsBoolean(indexPrefab + Property_PrefabAddCollider))
                        {
                            //CopyCollider(obj, ref newColliderObject, ref newPrefab, is2DColliders);
                            AddCollider(newPrefab, obj, obj.Type.Equals(Object_Type_Trigger), is2DColliders, zDepth);
                        }
                        else
                            // since custom properties are automatically added when a collider is added but this prefab has no collider, we must enforce them to be parsed
                            ApplyCustomProperties(newPrefab, obj);

                    }
                    else
                        // since custom properties are automatically added when a collider is added but this prefab has no collider, we must enforce them to be parsed
                        ApplyCustomProperties(newPrefab, obj);

                    if (obj.GetPropertyAsBoolean(indexPrefab + Property_PrefabFixColliderPosition))
                    {
                        // Mario: Fixed wrong position in instantiate prefabs
                        if(newColliderObject != null && newPrefab != null)
                            newPrefab.transform.position = newColliderObject.transform.position;
                    }

                    newPrefab.name = (addTileName ? (_mapName + "_") : "") + obj.Name;
                    int indexMessage = 1;
                    string prefabMensage = obj.GetPropertyAsString(indexPrefab + Property_PrefabSendMessage + indexMessage);
                    while (string.IsNullOrEmpty(prefabMensage) == false)
                    {
                        string[] menssage = prefabMensage.Split(new[] { '|' }, StringSplitOptions.None);
                        if (menssage.Length == 2)
                        {
                            newPrefab.BroadcastMessage(menssage[0], menssage[1]);
                        }
                        if (menssage.Length == 1)
                        {
                            newPrefab.BroadcastMessage(menssage[0]);
                        }
                        indexMessage++;
                        prefabMensage = obj.GetPropertyAsString(indexPrefab + Property_PrefabSendMessage + indexMessage);
                    }

                }
                else
                {
                    Debug.LogError("Prefab doesn't exist at: Resources/" + baseResourcePath + prefabName);
                }
                indexPrefab++;
            }
        }
示例#4
0
        public void Init(MapObject door)
        {
            if (door.HasProperty("name")) name = door.GetPropertyAsString("name");

            Out = Helper.TranslateDirection(door.GetPropertyAsString("out"));
            In = Helper.TranslateDirection(door.GetPropertyAsString("in"));

            ForceOut = door.HasProperty("force out") ? door.GetPropertyAsFloat("force out") : 1;
            ForceIn = door.HasProperty("force in") ? door.GetPropertyAsFloat("force in") : 1;

            GoTo = door.GetPropertyAsString("go to");
            Scene = door.GetPropertyAsString("scene");
        }