通过提前在鼠标经过的网格上实例化物体,鼠标离开该网格区域时再删除,实现了建造预览
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//新编码所需的信息 xz坐标 建筑ID 所占格数 /区分建筑码和地址码 地址码只记录xz坐标和是否被占用
//需增加功能 阻止重复建造 铲除建筑 建造时跟随鼠标吸附网格并虚化
///-----------------------------------------------------------------------------------------
/// Founction:
/// InvolvingKnowledge:
/// Author: AttouchAusturo
/// LatestReviseTime:
///-----------------------------------------------------------------------------------------
public class MultyGridBuild : MonoBehaviour
{
//建筑的代号
public int buildingID;
//建筑预制件 /提前指定
public GameObject followTestPrefab; //建造预览物体预制件 /暂定:小圆球
public GameObject tentPrefab; //帐篷
public GameObject warehousePrefab; //仓库
public GameObject ovenPrefab; //土灶
public GameObject towerPrefab; //塔
public GameObject workbenchPrefab; //工作台
public GameObject smelterPrefab; //熔炉
public GameObject grainsPrefab; //粮堆
//要建造的建筑和建筑实例 /非提前指定
public GameObject objectToBuild; //衔接选择建筑与建造
public GameObject objectToReBuild; //衔接选择建筑与重建
public GameObject buildings; //建造物实例化
public GameObject virtualBuilding; //预览建造物
//编码内容
public int positionX = 0, positionZ, reBuildBuildingID;
//公有的建筑码 /编码为建筑属性
public int allCode;
//建筑码存储地址 /编码后转字符串 /地址为网格属性
public int theAddress;
public string strTheAddress;
//射线获取坐标
private Ray ray;
private RaycastHit hit;
private Vector3 target;//鼠标点击位置的世界坐标
//坐标转换
private Vector3 groundArea;
private Vector3 recGroundArea;
//是否仍被选中 /未点击建造
public int beSelected = 0;
private void OnGUI()
{
GUILayout.BeginHorizontal("box");
GUILayout.Label("选择建筑");
if (GUILayout.Button("小帐篷")) { buildingID = 1; beSelected = 1; }//选择建筑
if (GUILayout.Button("仓库")) { buildingID = 2; beSelected = 1; }
if (GUILayout.Button("土灶")) { buildingID = 3; beSelected = 1; }
if (GUILayout.Button("瞭望塔")) { buildingID = 4; beSelected = 1; }
if (GUILayout.Button("工作台")) { buildingID = 5; beSelected = 1; }
if (GUILayout.Button("熔炉")) { buildingID = 6; beSelected = 1; }
if (GUILayout.Button("粮堆")) { buildingID = 7; beSelected = 1; }
if (GUILayout.Button("铲除")) {; }
GUILayout.EndHorizontal();
}
private void Start()
{
ClearAllBuildings();//清除存档内容为0
TryingMyRebuildMethodWithPlayerPrefs();//再次运行时重建
}
private void Update()
{
buildingID = CreateBuilding(buildingID);//建造功能函数 /目前包含功能 射线检测 坐标取整 生成建筑 生成编码并保存 建造中实时预览
}
//建造功能
private int CreateBuilding(int buildingID)
{
//衔接选择建筑与建造
if (buildingID == 1) objectToBuild = tentPrefab;
if (buildingID == 2) objectToBuild = warehousePrefab;
//if (buildingID == 3) ;
//if (buildingID == 4) ;
//if (buildingID == 5) ;
//if (buildingID == 6) ;
//if (buildingID == 7) ;
//如何区分两个因果关系的点击为两次点击 /暂时方案:区分左右键
if (buildingID != 0)
{
//如果未决定建造 /预览
if (beSelected != 0)
{
//鼠标在屏幕的位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//绘制出一条从相机射出的红色射线
Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
}
//获取点击位置的世界坐标
target = hit.point;
groundArea = new Vector3((int)target.x / 10 * 10 + 5, 0, (int)target.z / 10 * 10 + 5);//坐标取整并移至格子中心 /取整:/10*10 /取中:+5
//如果groundArea发生改变
if (groundArea != recGroundArea)
{
//摧毁上一个位置的对象
Destroy(virtualBuilding, 0);
//在格子中央生成该物体
virtualBuilding = Instantiate(followTestPrefab, groundArea, Quaternion.identity) as GameObject;
}
//记录鼠标位置变化
recGroundArea = groundArea;
}//预览
//当点击鼠标右键时 /决定建造
if (Input.GetMouseButtonDown(1))
{
//删除最后残留的预览物体
Destroy(virtualBuilding, 0);
//鼠标在屏幕的位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//绘制出一条从相机射出的红色射线
Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
}
target = hit.point;//获取点击位置的世界坐标
//判断建造的位置在存档中是否有障碍物
//取消预览
beSelected = 0;
//执行建造
groundArea = new Vector3((int)target.x / 10 * 10 + 5, 0, (int)target.z / 10 * 10 + 5);//坐标取整并移至格子中心 /取整:/10*10 /取中:+5
buildings = Instantiate(objectToBuild, groundArea, Quaternion.identity) as GameObject;//在格子中央生成该物体
//将新建筑编码
allCode = (int)target.x / 10 * 10000 + (int)target.z / 10 * 100 + buildingID;
//保存
TryingMyNewSavingMethodWithPlayerPrefs();
return buildingID = 0;
}//决定建造
}
return buildingID;
}//建造功能
//清除所有建筑
private void ClearAllBuildings()
{
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
PlayerPrefs.SetInt(strTheAddress, 0);
}
}
}//清除所有建筑
//保存建筑 /将Code解码来确定将其放入哪个地址中
private void TryingMyNewSavingMethodWithPlayerPrefs()
{
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
if (allCode / 10000 == i - 1 && allCode % 10000 / 100 == j - 1)
{
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
PlayerPrefs.SetInt(strTheAddress, allCode);
}
}
}
}//保存建筑
//取出所有地址中的值,赋给Code并进行解码
private void TryingMyRebuildMethodWithPlayerPrefs()
{
int allCode;
for (int i = 1; i < 21; i++)
{
for (int j = 1; j < 21; j++)
{
theAddress = 10000 + (i * 100) + j;
strTheAddress = theAddress.ToString();
allCode = PlayerPrefs.GetInt(strTheAddress);
AnalyzeCode(allCode);//解码
}
}
}//取出所有地址中的值,赋给Code并进行解码
//解码 并生成建筑
private void AnalyzeCode(int allCode)
{
if (allCode != 0)
{
positionX = allCode / 10000;
positionZ = allCode % 10000 / 100;
reBuildBuildingID = allCode % 100;
if (reBuildBuildingID == 1) objectToReBuild = tentPrefab;
if (reBuildBuildingID == 2) objectToReBuild = warehousePrefab;
buildings = Instantiate(objectToReBuild, new Vector3(positionX * 10 + 5, 0, positionZ * 10 + 5), Quaternion.identity) as GameObject;
}
}//解码 并生成建筑
}