您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
120 行
3.5 KiB
120 行
3.5 KiB
using System;
|
|
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 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 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 = 2;
|
|
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
|
|
});
|
|
}
|
|
}
|
|
}
|
|
private void Start()
|
|
{
|
|
centerInstance = center;
|
|
CreateGameObject(cube, RaycastableSceneConfig.cubeCount, RaycastableSceneConfig.cubeName);
|
|
CreateGameObject(ball, RaycastableSceneConfig.ballCount, RaycastableSceneConfig.ballName);
|
|
CreateGameObject(cylinder, RaycastableSceneConfig.cylinderCount, RaycastableSceneConfig.cylinderName);
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
// int count = closeObjects.Count;
|
|
// transform.Rotate(new Vector3(0,1,0), Time.deltaTime);
|
|
// for (int i = 0; i < count; i++)
|
|
// {
|
|
// closeObjects[i].transform.position()
|
|
// }
|
|
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
foreach (var obj in objects)
|
|
{
|
|
Destroy(obj.Value.obj);
|
|
}
|
|
}
|
|
|
|
public static void MoveCenter(string name)
|
|
{
|
|
var obj = objects.getOrDefault(name);
|
|
if (obj != null)
|
|
{
|
|
obj.obj.transform.position = obj.position;// centerInstance.position;
|
|
// closeObjects.Add(obj);
|
|
}
|
|
}
|
|
|
|
public static void MoveFar(string name)
|
|
{
|
|
var obj = objects.getOrDefault(name);
|
|
if (obj != null)
|
|
{
|
|
obj.obj.transform.position = RaycastableSceneConfig.far;
|
|
// closeObjects.Remove(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|