首页 > 其他分享 >Episode 08

Episode 08

时间:2023-03-21 11:01:50浏览次数:54  
标签:Map Episode mapSize 08 Editor using public MapGenerator

Tile Map——地图

MapGenerator

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapGenerator : MonoBehaviour
{
    public Transform tilePrefab;//瓦片预制体
    public Vector2 mapSize;//地图尺寸

    [Range(0f, 1f)]
    public float outlinePercent;//瓦片间隙

    void Start()
    {
        GenerateMap();
    }

    public void GenerateMap()
    {
        string holderName = "Generated Map";
        if (transform.Find(holderName))
        {
            DestroyImmediate(transform.Find(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;//创建Generated Map集合瓦片
        mapHolder.parent = transform;//将Generated Map父对象设置为为MapGenerator

        //循环创建瓦片
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x,0 , -mapSize.y / 2 + 0.5f + y);//居中
                Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90)) as Transform;
                newTile.localScale = Vector3.one * (1 - outlinePercent);
                newTile.parent = mapHolder;//将瓦片父对象设置为Generated Map
            }
        }
    }
}

MapEditor

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor (typeof (MapGenerator))]
public class MapEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        MapGenerator map = target as MapGenerator;
        map.GenerateMap();
    }
}

在开发过程中常常需要在编辑器上对某个特定的 Component 进行一些操作,一般是在Editor文件夹中新建一个继承自Editor的脚本,之后编辑继承自UnityEditor.Editor,这里注意是必须在类上加入 [CustomEditor(typeof(编辑器脚本绑定的 Monobehavior 类)]然后重写它的 OnInspectorGUI 方法,而一般而言在Editor类中操作变量有两种方式:

  • 通过直接访问或者函数调用改动Monobehaviour的变量(文中即是)
  • 通过Editor类中的serializedObject来改动对应变量

拓展:

标签:Map,Episode,mapSize,08,Editor,using,public,MapGenerator
From: https://www.cnblogs.com/Felix-Fu/p/17239193.html

相关文章