Beispiel #1
0
        /// <summary>
        /// Applies to gameObject any custom X-UniTMX properties present on obj
        /// </summary>
        /// <param name="gameObject">GameObject to apply custom properties to</param>
        /// <param name="obj">MapObject to read custom properties from</param>
        public void ApplyCustomProperties(GameObject gameObject, MapObject obj)
        {
            // nothing to do here...
            if (gameObject == null || obj == null)
                return;

            // Set a layer number for gameObject
            if (obj.HasProperty(Property_Layer))
                gameObject.layer = obj.GetPropertyAsInt(Property_Layer);

            if (obj.HasProperty(Property_LayerName))
                gameObject.layer = LayerMask.NameToLayer(obj.GetPropertyAsString(Property_LayerName));

            // Add a tag for gameObject
            if (obj.HasProperty(Property_Tag))
                gameObject.tag = obj.GetPropertyAsString(Property_Tag);
            // Add Components for this gameObject
            int c = 1;
            while (obj.HasProperty(Property_AddComponent + c))
            {
                UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(gameObject, "Assets/X-UniTMX/Code/Map.cs (2684,5)", obj.GetPropertyAsString(Property_AddComponent + c));
                c++;
            }
            c = 1;
            while (obj.HasProperty(Property_SendMessage + c))
            {
                string messageToSend = obj.GetPropertyAsString(Property_SendMessage + c);
                string[] menssage = messageToSend.Split('|');
                if (menssage.Length == 2)
                {
                    gameObject.BroadcastMessage(menssage[0], menssage[1]);
                }
                if (menssage.Length == 1)
                {
                    gameObject.BroadcastMessage(menssage[0]);
                }
                c++;
            }

            if (gameObject.GetComponent<Renderer>() != null)
            {
                if (obj.HasProperty(Property_SortingLayerName))
                    gameObject.GetComponent<Renderer>().sortingLayerName = obj.GetPropertyAsString(Property_SortingLayerName);

                if (obj.HasProperty(Property_SortingOrder))
                    gameObject.GetComponent<Renderer>().sortingOrder = obj.GetPropertyAsInt(Property_SortingOrder);

                if (obj.HasProperty(Property_SetMaterialColor))
                {
                    string[] splitColor = obj.GetPropertyAsString(Property_SetMaterialColor).Split(',');
                    if (splitColor.Length >= 1)
                    {
                        gameObject.GetComponent<Renderer>().material = new Material(BaseTileMaterial);
                        gameObject.GetComponent<Renderer>().material.color = new Color32(
                            ((byte)(int.Parse(string.IsNullOrEmpty(splitColor[0]) ? "255" : splitColor[0]))),
                            splitColor.Length >= 2 ? ((byte)(int.Parse(splitColor[1]))) : (byte)255,
                            splitColor.Length >= 3 ? ((byte)(int.Parse(splitColor[2]))) : (byte)255,
                            splitColor.Length >= 4 ? ((byte)(int.Parse(splitColor[3]))) : (byte)255);
                    }
                }
            }
        }
        /// <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;
        }
Beispiel #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++;
            }
        }
Beispiel #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");
        }