コード例 #1
0
        /// <summary>
        /// Gets a string containing the Xml markup for the values in the <see cref="model"/> parameter.
        /// </summary>
        /// <param name="model">The model to be converted to a Xml string.</param>
        /// <returns>Returns a string containing the Xml markup for the values in the <see cref="model"/> parameter.</returns>
        /// <exception cref="ArgumentNullException">If <see cref="model"/> is null.</exception>
        public static string ToXml(this DrawRuleModel model)
        {
            // ensure model has a reference
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            // get the source path for the model
            var sourcePrefab = Helpers.GetSourcePrefab(model.Prefab);

            // build a list of alternate prefab
            var alternates = from item in model.Alternates
                             let prefabPath = Helpers.GetSourcePrefab(item.Prefab)
                             where item != null && item.Prefab != null
                             select string.Format(new string(' ', 14) + "<alternate id=\"{0}\">\r\n" +
                                                                        "<prefabsource><![CDATA[{1}]]></prefabsource>\r\n" +
                                                                        "</alternate>", AssetDatabase.AssetPathToGUID(prefabPath), prefabPath);

            // return a Xml string containing the data from the model
            return string.Format(
                "    <rule>\r\n" +
                "        <name><![CDATA[{0}]]></name>\r\n" +
                "        <enabled>{1}</enabled>\r\n" +
                "        <prefab>{2}</prefab>\r\n" +
                "        <prefabsource><![CDATA[{11}]]></prefabsource>\r\n" +
                "        <upperneighbors enabled=\"{3}\" states=\"{4}\"></upperneighbors>\r\n" +
                "        <neighbors states=\"{5}\"></neighbors>\r\n" +
                "        <lowerneighbors enabled=\"{6}\" states=\"{7}\"></lowerneighbors>\r\n" +
                "        <alternates>\r\n{8}\r\n" +
                "        </alternates>\r\n" +
                "        <alloworiginal>{9}</alloworiginal>\r\n" +
                "        <description><![CDATA[{10}]]></description>\r\n" +
                "        <category><![CDATA[{12}]]></category>\r\n" +
                "    </rule>",
                string.IsNullOrEmpty(model.Name) ? string.Empty : model.Name.Trim(),
                model.Enabled,
                model.Prefab == null ? string.Empty : AssetDatabase.AssetPathToGUID(Helpers.GetSourcePrefab(model.Prefab)).ToString(CultureInfo.InvariantCulture),
                model.NeighborsUpperEnabled,
                string.Join(string.Empty, model.NeighborsUpper.Select(x => x ? "1" : "0").ToArray()),
                string.Join(string.Empty, model.Neighbors.Select(x => x ? "1" : "0").ToArray()),
                model.NeighborsLowerEnabled,
                string.Join(string.Empty, model.NeighborsLower.Select(x => x ? "1" : "0").ToArray()),
                model.Alternates == null ? string.Empty : string.Join("\r\n", alternates.ToArray()),
                model.AllowOriginal,
                string.IsNullOrEmpty(model.Description) ? string.Empty : model.Description.Trim(),
                sourcePrefab ?? string.Empty,
                string.IsNullOrEmpty(model.Category) ? string.Empty : model.Category.Trim());
        }
        /// <summary>
        /// Called by <see cref="GridMapEditor"/> to update the tool on every call to OnSceneGUI.
        /// </summary>
        public void Update()
        {
            if (!this.isActive)
            {
                return;
            }

            // do not draw if not the view tool
            if (Tools.current != Tool.View)
            {
                return;
            }

            var current = Event.current;

            // check if mouse is up and button is 0 and if so exit
            if (!(current.type == EventType.MouseUp & current.button == 0))
            {
                return;
            }

            current.Use();

            // Calculate the position of the mouse over the grid layer
            var gridPosition = this.editor.GetMouseGridPosition();

            var item = this.editor.GameObjects.Get(gridPosition.X, gridPosition.Y, this.editor.ActiveLayer);

            if (item == null)
            {
                return;
            }

            // generate output data
            var data = string.Format(
                "Name: \"{0}\"\r\n" +
                "PrefabType: {1}\r\n" +
                "Source Path: \"{2}\"\r\n" +
                "Shared Material: \"{3}\"\r\n" +
                "Material: \"{4}\"\r\n",
                item.name,
                Enum.GetName(typeof(PrefabType), PrefabUtility.GetPrefabType(item)),
                Helpers.GetSourcePrefab(item),
                item.renderer != null && item.renderer.sharedMaterial != null ? item.renderer.sharedMaterial.name : string.Empty,
                item.renderer != null && item.renderer.material != null ? item.renderer.material.name : string.Empty);

            Debug.Log(data);
        }
コード例 #3
0
        /// <summary>
        /// Called by <see cref="GridMapEditor"/> to update the tool on every call to OnSceneGUI.
        /// </summary>
        public void Update()
        {
            // do not pick if not the view tool or is not active
            if (!this.isActive || Tools.current != Tool.View)
            {
                return;
            }

            var current = Event.current;

            // check if mouse is down or dragging and if so draw
            if (current.type == EventType.MouseUp && current.button == 0)
            {
                // eat the event
                current.Use();

                // Calculate the position of the mouse over the grid layer
                var gridPosition = this.editor.GetMouseGridPosition();

                var item = this.editor.GameObjects.Get(gridPosition.X, gridPosition.Y, this.editor.ActiveLayer);

                var sourcePrefab = Helpers.GetSourcePrefab(item);
                var instance     = GridMappingService.Instance;
                if (!string.IsNullOrEmpty(sourcePrefab) && File.Exists(sourcePrefab.Trim()))
                {
                    // load a reference to the source prefab
                    var temp = AssetDatabase.LoadAssetAtPath(sourcePrefab, typeof(GameObject)) as GameObject;
                    if (item != null && item.renderer != null)
                    {
                        instance.CurrentMaterial = item.renderer.sharedMaterial;
                    }

                    item = temp;
                }

                // set current prefab
                instance.CurrentPrefab = item;
                this.editor.SetPrefabSelectionToCustom();

                this.editor.Repaint();
            }
        }