• 射线生成Cube及删除

void CreateCube()
    {
        Ray ray;
        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                Vector3 orign = hit.collider.bounds.center; //得到点击到的cube中心
                Vector3 hitPoint = hit.point;
                Vector3 hitPointLocal = hit.collider.gameObject.transform.InverseTransformPoint(hitPoint);
                //世界坐标下一个点 你想知道这个点如果是在物体点击Cube的坐标下是一个什么位置 用InverseTransformPoint
                float x = Mathf.Abs(hitPointLocal.x);
                float y = Mathf.Abs(hitPointLocal.y);
                float z = Mathf.Abs(hitPointLocal.z);
                if (x < 0.5) { hitPointLocal = new Vector3(0, hitPointLocal.y, hitPointLocal.z); }
                if (y < 0.5) { hitPointLocal = new Vector3(hitPointLocal.x, 0, hitPointLocal.z); }
                if (z < 0.5) { hitPointLocal = new Vector3(hitPointLocal.x, hitPointLocal.y, 0); }
                hitPoint = hit.collider.gameObject.transform.TransformPoint(hitPointLocal);
                //物体a的坐标内有一个3,3,3的点  你想知道这个点在世界坐标的位置 就应该用TransformPoint
                Vector3 direction = Vector3.Normalize(hitPoint – orign); //得到目标位置和父cube的向量
                Vector3 spwanPoint = hitPoint + direction * _cubeDistance;
                GameObject go = Instantiate(obj[ColorNumber], spwanPoint, hit.collider.gameObject.transform.rotation)as GameObject;
                go.transform.parent = CubeSavePos.transform;
                newCube.Pos = spwanPoint.x.ToString() + “|” + spwanPoint.y.ToString() + “|” +spwanPoint.z.ToString();
                newCube.Type = ColorNumber;
                JsonSave.jsonSave.SaveBuildingData(newCube);
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                if (hit.transform.name == “EarthCube”)
                    return;
                newCube.Pos = hit.transform.position.x.ToString() + “|” + hit.transform.position.y.ToString() + “|” + hit.transform.position.z.ToString();
                newCube.Type = 0;
                JsonSave.jsonSave.CloseJsonCube(newCube);
                Destroy(hit.transform.gameObject);
            }
        }
    }
  • 摄像机添加一个360旋转

    void CameraRotate()
    {
        if (Input.GetMouseButton(0))
        {
            x += Input.GetAxis(“Mouse X”) * RspeedX;
            y -= Input.GetAxis(“Mouse Y”) * RspeedX;
        }
        Quaternion rotateAngle = Quaternion.Euler(y, x, 0);//摄像机偏转角度 
        transform.rotation = rotateAngle;//让摄像机始终转向物品 
        Vector3 direction = new Vector3(0, camHeight, -distance);//摄像机距离物品的距离 
        transform.position = target.position + rotateAngle * direction;//计算旋转多少角度摄像机需要偏移多少 
        float roller = Input.GetAxis(“Mouse ScrollWheel”);
        if (roller > 0)
        {
            if (Camera.main.fieldOfView <= 18)
                return;
            if (Camera.main.fieldOfView > 2)
                Camera.main.fieldOfView -= 6;
            if (Camera.main.orthographicSize >= 1)
                Camera.main.orthographicSize -= 0.5F;
        }
        else if(roller<0)
        {
            if (Camera.main.fieldOfView <= 120)
                Camera.main.fieldOfView += 6;
            if (Camera.main.orthographicSize <= 20)
                Camera.main.orthographicSize += 0.5F;
        }
    }
  • 利用Json保存方块信息并加载

    //读取信息
    public void LoadBuildingData()
    {
        string filePath = System.IO.Path.Combine(GetResourcesPath(), “CubesJson.txt”);
        if (File.Exists(filePath)) //判断这个文件是否存在
        {
            string text = File.ReadAllText(filePath);
            _cubeList = JsonMapper.ToObject<List<CubeInfo>>(text);
            if (_cubeList.Count <= 1)
                return;
            foreach (var cube in _cubeList)
            {
                string[] xyz = cube.Pos.Split(‘|’);
                Vector3 Pos = new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2]));
                CameraMove.cameraMove.InstantiateCube(cube.Type, Pos);
            }
        }
    }
    //保存信息
    public void SaveBuildingData(CubeInfo data)
    {
        string filePath = GetResourcesPath() + @”/CubesJson.txt”;
        if (!File.Exists(filePath))
        {
            _cubeList.Add(data);
        }
        else
        {
            //替换相同的
            //bool bFind = false;
            //for (int i = 0; i < _cubeList.Count; i++)
            //{
            //    CubeInfo saveData = _cubeList[i];
            //    if (data.Pos == saveData.Pos)
            //    {
            //        saveData.Pos = data.Pos;
            //        saveData.Type = data.Type;
            //        bFind = true;
            //        break;
            //    }
            //}
            //if (!bFind)
            //替换相同的
            _cubeList.Add(data);
        }
        SaveToJson();
    }
    void SaveToJson()
    {
        string filePath = GetResourcesPath() + @”/CubesJson.txt”;
        FileInfo file = new FileInfo(filePath);
        StreamWriter sw = file.CreateText();
        string json = JsonMapper.ToJson(_cubeList);
        sw.WriteLine(json);
        sw.Close();
        sw.Dispose();
    }
    public void CloseJsonCube(CubeInfo DeleteCubeInfo)
    {
        CubeInfo deleteCube = _cubeList.Find((CubeInfo M) => { return (M.Pos == DeleteCubeInfo.Pos); });
        _cubeList.Remove(deleteCube);
        SaveToJson();
    }
    //不同平台的预编译
    public static string GetResourcesPath()
    {
        if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
        {
            return Application.streamingAssetsPath + “/”;
        }
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            //return Application.dataPath + “/Raw/”;
            return Application.persistentDataPath + “/”;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            //return “jar:file://” + Application.dataPath + “!/assets/”;
            return Application.persistentDataPath + “/”;
        }
        return “”;
    }