private void SpawnObjectWithRay() { if (Input.GetMouseButtonDown(0)) { Ray ray = mainCam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity)) { Color color = new Color(Random.Range(0.5f, 1f), Random.Range(0.5f, 1f), Random.Range(0.5f, 1f)); IObjectPlaceCommand command = new PlaceCubeCommand(hitInfo.point, color, prefab); ObjectPlaceCommandInvoker.AddCommand(command); } } }
private void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = mainCam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity)) { Color c = new Color(Random.Range(0.5f, 1f), Random.Range(0.5f, 1f), Random.Range(0.5f, 1f)); ICommand command = new PlaceCubeCommand(hitInfo.point, c, cubePrefab); CommandInvoker.AddCommand(command); } } }
void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity)) { Color c = new Color(Random.Range(0.5f, 1), Random.Range(0.5f, 1), Random.Range(0.5f, 1)); // 传统写法 //CubePlacer.PlaceCube(hitInfo.point, c, cubePrefab); // 命令模式 ICommand command = new PlaceCubeCommand(hitInfo.point, c, cubePrefab); CommandInvoker.AddCommand(command); } } }