您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
141 行
4.2 KiB
141 行
4.2 KiB
using System.Collections.Generic;
|
|
using Unity.UIWidgets.foundation;
|
|
using UnityEngine;
|
|
|
|
namespace UIWidgetsSample.RaycastableScene
|
|
{
|
|
public class ObjectItem
|
|
{
|
|
public GameObject obj;
|
|
public Vector3 position;
|
|
public string name;
|
|
public bool active;
|
|
}
|
|
|
|
public class GameObjectManager : MonoBehaviour
|
|
{
|
|
public GameObject cube;
|
|
public GameObject ball;
|
|
public GameObject cylinder;
|
|
|
|
public Transform center;
|
|
public static Transform centerInstance;
|
|
|
|
public static Dictionary<string, ObjectItem> objects = new Dictionary<string, ObjectItem>();
|
|
|
|
public static Vector3? cameraPosition;
|
|
|
|
public static Camera cameraInstance;
|
|
|
|
public Camera camera;
|
|
|
|
|
|
// public static List<GameObject> closeObjects = new List<GameObject>();
|
|
|
|
public Vector3 NewPosition()
|
|
{
|
|
int count = objects.Count;
|
|
int sq = (int) Mathf.Sqrt(count);
|
|
int remain = count - sq * sq;
|
|
int r = 0;
|
|
int c = 0;
|
|
if (remain <= sq)
|
|
{
|
|
r = sq;
|
|
c = remain;
|
|
}
|
|
else
|
|
{
|
|
r = 2 * sq - remain;
|
|
c = sq;
|
|
}
|
|
|
|
var s = RaycastableSceneConfig.scaterScale;
|
|
return center.position + new Vector3(r * s, c * s, 0);
|
|
}
|
|
|
|
public void CreateGameObject(GameObject obj, int count, string name)
|
|
{
|
|
int sq = (int)Mathf.Sqrt(count);
|
|
if (obj)
|
|
{
|
|
for (int i = 0; i <count; i++)
|
|
{
|
|
int r = i % sq;
|
|
int c = i / sq;
|
|
int s = 1;
|
|
var position = NewPosition();
|
|
var newObj = Instantiate(obj, position, Quaternion.identity);
|
|
newObj.name = $"{name}{i + 1}";
|
|
objects.Add(newObj.name, new ObjectItem()
|
|
{
|
|
obj = newObj,
|
|
name = newObj.name,
|
|
position = newObj.transform.position,
|
|
active = true
|
|
});
|
|
}
|
|
}
|
|
}
|
|
private void OnEnable()
|
|
{
|
|
centerInstance = center;
|
|
cameraInstance = camera;
|
|
CreateGameObject(cube, RaycastableSceneConfig.cubeCount, RaycastableSceneConfig.cubeName);
|
|
CreateGameObject(ball, RaycastableSceneConfig.ballCount, RaycastableSceneConfig.ballName);
|
|
CreateGameObject(cylinder, RaycastableSceneConfig.cylinderCount, RaycastableSceneConfig.cylinderName);
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
if (cameraPosition != null )
|
|
{
|
|
var dif = cameraPosition.Value - camera.transform.position;
|
|
if (dif.sqrMagnitude > RaycastableSceneConfig.delta)
|
|
{
|
|
camera.transform.position = dif.normalized * (RaycastableSceneConfig.cameraSpeed * Time.deltaTime)
|
|
+ camera.transform.position;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
foreach (var obj in objects)
|
|
{
|
|
Destroy(obj.Value.obj);
|
|
}
|
|
}
|
|
|
|
public static void FocusOnObject(string name)
|
|
{
|
|
var obj = objects.getOrDefault(name);
|
|
if (obj != null && obj.active)
|
|
{
|
|
cameraPosition = new Vector3(obj.position.x, obj.position.y, cameraInstance.transform.position.z);
|
|
}
|
|
}
|
|
|
|
public static void MoveCenter(string name)
|
|
{
|
|
var obj = objects.getOrDefault(name);
|
|
if (obj != null)
|
|
{
|
|
obj.obj.transform.position = obj.position;
|
|
obj.active = true;
|
|
}
|
|
}
|
|
|
|
public static void MoveFar(string name)
|
|
{
|
|
var obj = objects.getOrDefault(name);
|
|
if (obj != null)
|
|
{
|
|
obj.obj.transform.position = RaycastableSceneConfig.far;
|
|
obj.active = false;
|
|
}
|
|
}
|
|
}
|
|
}
|