public void SetupButtons(string[] modelUrls, ArController obj) { char[] separators = new char[] { '\\', '/' }; foreach (var url in modelUrls) { if (!string.IsNullOrEmpty(url)) { Button b = Instantiate(templateButton); TextMeshProUGUI buttonText = b.GetComponentInChildren <TextMeshProUGUI>(); if (buttonText) { string[] broken = url.Split(separators); if (broken.Length > 0) { buttonText.text = broken[broken.Length - 1]; } } b.transform.parent = templateButton.transform.parent; b.transform.localScale = Vector3.one; b.onClick.AddListener(delegate() { obj.LoadAndSpawnGLTFModel(url); }); b.gameObject.SetActive(true); } } }
/// <summary> /// 根据Resouse目录下的图片初始化背包中物品 /// </summary> /// <param name="imagePath">Image path.</param> void CreateCell(string imagePath) { GameObject go = new GameObject(imagePath); go.AddComponent <Image>(); BagItem bagItem = go.AddComponent <BagItem>(); bagItem.SetImage(imagePath); bagItem.SetMyPointerClick(() => { if (_putArObjects.Count >= 1) { print("只可放置一个模型"); return; } Object model = Resources.Load("Model/" + imagePath.Split('/')[1]); print("bagItem click:" + gameObject); print("Model/" + imagePath.Split('/')[1]); print("Model:" + model); var modelInfo = ArController.CreateArModel(model, new Vector3(0, 0, 2), Quaternion.Euler(new Vector3())); Vector3 size = ArUtils.GetObjectSizeByCollider(modelInfo.ArGameObject); modelInfo.ArGameObject.transform.position = new Vector3(0, -size.y / 2, 2 * size.z); // 脚本初始化都为false 当用户选择某个模型后添加对此模型的控制 RotateAndUpDown raud = modelInfo.ArGameObject.AddComponent <RotateAndUpDown>(); raud.enabled = false; TransfromAroundAndDistance tad = modelInfo.ArGameObject.AddComponent <TransfromAroundAndDistance>(); tad.SetCamera(_firstPersonCamera.GetComponent <Camera>()); tad.enabled = false; DoubleClickChangeStatus dccs = modelInfo.ArGameObject.AddComponent <DoubleClickChangeStatus>(); dccs.SetCamera(_firstPersonCamera.GetComponent <Camera>()); dccs.enabled = false; // 设置模型附加的脚本信息 modelInfo.RotateAndUpDown = raud; modelInfo.TransfromAroundAndDistance = tad; modelInfo.DoubleClickChangeStatus = dccs; modelInfo.ArGameObject.transform.RotateAround(_firstPersonCamera.transform.position, Vector3.up, _firstPersonCamera.transform.rotation.eulerAngles.y); // 更改名字作为每个物体不同的标识 modelInfo.ArGameObject.name = $"{modelInfo.ArGameObject.name.Split('(')[0]}({modelId++})"; _putArObjects.Add(modelInfo); onMyItemClick?.Invoke(modelInfo.ArGameObject, _putArObjects); }); go.transform.SetParent(transform, false); }
private void Awake() { Instance = this; }