用笔刷的形式实现地图编辑器

14 篇文章 0 订阅

我们策划需要在方块地图中刷不同的单元格,正好之前对类似于魔兽编辑器的功能比较感兴趣,就用射线+物体生成的方式实现了下,实现效果截图如下:

清除效果:

实现代码如下:

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

public class CreatMapUpdate : MonoBehaviour
{
    public GameObject[] objs;

    public Button[] btns;

    public int creatID = 0;

    public float length = 2.5f;
    public float width = 2.5f;

    public Dictionary<Vector2, GameObject> dic = new Dictionary<Vector2, GameObject>();

    // Start is called before the first frame update
    void Start()
    {       
        for (var _i = 0; _i < btns.Length; _i++)
        {
            var _j = _i;
            btns[_i].onClick.AddListener(delegate { Debug.LogError(_j); creatID = _j - 1; });
        }
    }

    // Update is called once per frame
    void Update()
    {
        
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition/*+new Vector3(15,0,-15)*/);
            RaycastHit hit;
            //判断是否碰撞到物体
            bool isCollider = Physics.Raycast(ray, out hit) && hit.transform.tag == "TouchCreat";
            if (isCollider)
            {
                var _creatPos = new Vector2((int)(hit.point.x / length), (int)(hit.point.z / width));
                //Debug.Log(hit.point +"   "+_creatPos);

                GameObject.Find("指示球").transform.position = hit.point;
                //以0,0点为中心,将地图切割为n个小地图块

                if (dic.ContainsKey(_creatPos))
                {
                    var _obj = dic[_creatPos];
                    if (creatID == -1)
                    {
                        dic.Remove(_creatPos);
                        Destroy(_obj);
                    }
                    else if (_obj.name != objs[creatID].name)
                    {
                        Destroy(_obj);
                        _obj = CreatObj(objs[creatID]);
                        dic[_creatPos] = _obj;
                        _obj.transform.position = new Vector3(_creatPos.x * length, 0, _creatPos.y * length);
                        _obj.transform.SetParent(transform);
                    }
                }
                else if (creatID >= 0)
                {
                    var _obj = CreatObj(objs[creatID]);
                    dic.Add(_creatPos, _obj);
                    _obj.transform.localPosition= new Vector3(_creatPos.x * length, 0, _creatPos.y * length);
                    _obj.transform.SetParent(transform);
                    //Debug.Log(objs.Length + "  " + creatID);
                }
               
            }
        }
       
    }


    public GameObject CreatObj(GameObject _oo)
    {
        var _obj = GameObject.Instantiate(_oo) as GameObject;
        _obj.name = _oo.name;
        return _obj;
    }
}

工程资源下载地址:地图笔刷编辑器.unitypackage-C#文档类资源-CSDN文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值