using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine; public class SnailPanel : MonoBehaviour { public static SnailPanel Instance { get; private set; } public GameObject snailPrefab; // 蜗牛的预制体 public float width ; // 分布区域的宽度 public float height ; // 分布区域的高度 public Vector2 startPosition = new Vector2(0f, 0f); // 起始位置 private UserSnail usersnail; public List Snails; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } // Start is called before the first frame update async void Start() { queryUserSnailCount queryUserSnailCount = new queryUserSnailCount(); //9.2查询我的工厂未绑定的蜗牛数,初始化界面 usersnail = await queryUserSnailCount.QueryUserSnailCount(); SpawnSnails(); Debug.Log(usersnail.data.Count); } // 生成指定数量的蜗牛物体并分布在指定范围内 void SpawnSnails( ) { for (int i = 0; i < usersnail.data.Count&&i<=10; i++) { float x = startPosition.x + (Random.Range(-(width / 2), (width / 2))) ; float y = startPosition.y + (Random.Range(-(height / 2), (height / 2))); Vector3 spawnPosition = new Vector3(x, y, 0f); // GameObject ob =Instantiate(snailPrefab); ob.transform.SetParent(transform); ob.transform.localPosition = spawnPosition; Snailmove snailmove = ob.GetComponent(); snailmove.id = usersnail.data[i].id; Debug.Log(usersnail.data[i].id); Snails.Add(ob); } } }