/// <summary>
        /// Applies to a GameObject any custom property found in PropertyCollection
        /// </summary>
        /// <param name="gameObject">GameObject to apply custom properties to</param>
        /// <param name="properties">PropertyCollection to read custom properties from</param>
        public static void ApplyCustomProperties(GameObject gameObject, PropertyCollection properties)
        {
            // nothing to do here...
            if (gameObject == null || properties == null)
                return;

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

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

            // Add a tag for gameObject
            if (properties.HasProperty(Map.Property_Tag))
                gameObject.tag = properties.GetPropertyAsString(Map.Property_Tag);

            int c = 1;
            // Unity 5 Removed AddComponent("string")...
            #if !UNITY_5
            // Add Components for this gameObject
            while (properties.HasProperty(Map.Property_AddComponent + c))
            {
                try
                {
                    gameObject.AddComponent(properties.GetPropertyAsString(Map.Property_AddComponent + c));
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
                c++;
            }
            c = 1;
            #endif
            // Can only send messages while playing
            if (Application.isPlaying)
            {
                while (properties.HasProperty(Map.Property_SendMessage + c))
                {
                    string messageToSend = properties.GetPropertyAsString(Map.Property_SendMessage + c);
                    string[] menssage = messageToSend.Split('|');
                    if (menssage.Length == 2)
                    {
                        gameObject.BroadcastMessage(menssage[0], menssage[1], SendMessageOptions.DontRequireReceiver);
                    }
                    if (menssage.Length == 1)
                    {
                        gameObject.BroadcastMessage(menssage[0], SendMessageOptions.DontRequireReceiver);
                    }
                    c++;
                }
            }
            Renderer renderer = gameObject.GetComponent<Renderer>();
            if (renderer != null)
            {
                if (properties.HasProperty(Map.Property_SortingLayerName))
                    renderer.sortingLayerName = properties.GetPropertyAsString(Map.Property_SortingLayerName);

                if (properties.HasProperty(Map.Property_SortingOrder))
                    renderer.sortingOrder = properties.GetPropertyAsInt(Map.Property_SortingOrder);

                if (properties.HasProperty(Map.Property_SetMaterialColor))
                {
                    string[] splitColor = properties.GetPropertyAsString(Map.Property_SetMaterialColor).Split(',');
                    if (splitColor.Length >= 1)
                    {
                        Material mat;
            #if !UNITY_5
                        mat = new Material(Shader.Find("Diffuse"));
            #else
                        mat = new Material(Shader.Find("Standard"));
                        mat.SetFloat("_Mode", 3);
                        mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                        mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                        mat.SetInt("_ZWrite", 0);
                        mat.DisableKeyword("_ALPHATEST_ON");
                        mat.EnableKeyword("_ALPHABLEND_ON");
                        mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                        mat.renderQueue = 3000;
            #endif
                        mat.SetColor("_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));
                        renderer.material = mat;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Applies to a GameObject any custom property found in PropertyCollection
        /// </summary>
        /// <param name="gameObject">GameObject to apply custom properties to</param>
        /// <param name="properties">PropertyCollection to read custom properties from</param>
        public static void ApplyCustomProperties(GameObject gameObject, PropertyCollection properties)
        {
            // nothing to do here...
            if (gameObject == null || properties == null)
                return;

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

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

            // Add a tag for gameObject
            if (properties.HasProperty(Map.Property_Tag))
                gameObject.tag = properties.GetPropertyAsString(Map.Property_Tag);

            int c = 1;
            // Unity 5 Removed AddComponent("string")...
            #if !UNITY_5
            // Add Components for this gameObject
            while (properties.HasProperty(Map.Property_AddComponent + c))
            {
                try
                {
                    gameObject.AddComponent(properties.GetPropertyAsString(Map.Property_AddComponent + c));

                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
                c++;
            }
            c = 1;
            #else
            // Add Components for this gameObject
            while (properties.HasProperty(Map.Property_AddComponent + c))
            {
                try
                {
                    //gameObject.AddComponent(System.Type.GetType(properties.GetPropertyAsString(Map.Property_AddComponent + c)));
                    //gameObject.AddComponent(System.Type.GetType("Door")) as Door;

                    if(properties.GetPropertyAsString(Map.Property_AddComponent + c) == "Door"){
                        gameObject.AddComponent<Door>();
                    }else if(properties.GetPropertyAsString(Map.Property_AddComponent + c) == "CollisionLevel"){
                        gameObject.AddComponent<CollisionLevel>();
                    }else if(properties.GetPropertyAsString(Map.Property_AddComponent + c) == "HidePlayer"){
                        gameObject.AddComponent<HidePlayer>();
                    }

                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
                c++;
            }
            c = 1;

            //Set variables for Component Door
            Door door;
            if(door = gameObject.GetComponent<Door>()){

                Debug.Log("HAS DOOR");

                Property force;

                //Debug.Log(gameObject.name+" FORCE "+force.RawValue.ToString());

                if(properties.TryGetValue(Map.Property_ForceIn, out force)){
                    door.ForceIn = float.Parse(force.RawValue);
                    Debug.Log("ForceIn "+force.RawValue);
                }

                if(properties.TryGetValue(Map.Property_ForceOut, out force)){
                    door.ForceOut = float.Parse(force.RawValue);
                    Debug.Log("ForceOut "+force.RawValue);
                }

                if(properties.TryGetValue(Map.Property_Goto, out force)){
                    door.GoTo = force.RawValue;
                    Debug.Log("Goto "+force.RawValue);
                }

                if(properties.TryGetValue(Map.Property_In, out force)){
                    door.In = (Direction) System.Enum.Parse(typeof(Direction), force.RawValue, true);
                    Debug.Log("In "+force.RawValue);
                }

                if(properties.TryGetValue(Map.Property_Out, out force)){
                    door.Out = (Direction) System.Enum.Parse(typeof(Direction), force.RawValue, true);
                    Debug.Log("Out "+force.RawValue);
                }
            }

            #endif
            // Can only send messages while playing
            if (Application.isPlaying)
            {
                while (properties.HasProperty(Map.Property_SendMessage + c))
                {
                    string messageToSend = properties.GetPropertyAsString(Map.Property_SendMessage + c);
                    string[] menssage = messageToSend.Split('|');
                    if (menssage.Length == 2)
                    {
                        gameObject.BroadcastMessage(menssage[0], menssage[1], SendMessageOptions.DontRequireReceiver);
                    }
                    if (menssage.Length == 1)
                    {
                        gameObject.BroadcastMessage(menssage[0], SendMessageOptions.DontRequireReceiver);
                    }
                    c++;
                }
            }
            Renderer renderer = gameObject.GetComponent<Renderer>();
            if (renderer != null)
            {
                if (properties.HasProperty(Map.Property_SortingLayerName))
                    renderer.sortingLayerName = properties.GetPropertyAsString(Map.Property_SortingLayerName);

                if (properties.HasProperty(Map.Property_SortingOrder))
                    renderer.sortingOrder = properties.GetPropertyAsInt(Map.Property_SortingOrder);

                if (properties.HasProperty(Map.Property_SetMaterialColor))
                {
                    string[] splitColor = properties.GetPropertyAsString(Map.Property_SetMaterialColor).Split(',');
                    if (splitColor.Length >= 1)
                    {
                        Material mat;
            #if !UNITY_5
                        mat = new Material(Shader.Find("Diffuse"));
            #else
                        mat = new Material(Shader.Find("Standard"));
                        mat.SetFloat("_Mode", 3);
                        mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                        mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                        mat.SetInt("_ZWrite", 0);
                        mat.DisableKeyword("_ALPHATEST_ON");
                        mat.EnableKeyword("_ALPHABLEND_ON");
                        mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                        mat.renderQueue = 3000;
            #endif
                        mat.SetColor("_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));
                        renderer.material = mat;
                    }
                }
            }
        }