最近遇到了一个问题DropDown的位置不能适应屏幕大小,自己想了个办法,请大家批评指正。
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
public class InitSerialPortDropdown : MonoBehaviour
{
/// <summary>
/// Dropdown-TextMeshPro
/// </summary>
public TMPro.TMP_Dropdown serialPortDropDown;
public Camera _MainCamera = null;
// Start is called before the first frame update
void Start()
{
InitDropdown();
}
// Update is called once per frame
private void InitDropdown()
{
//清空默认节点
serialPortDropDown.options.Clear();
foreach (string serialPortName in SerialPortManager.GetPort())
{
//初始化
TMP_Dropdown.OptionData op = new TMP_Dropdown.OptionData();
op.text = serialPortName;
serialPortDropDown.options.Add(op);
}
}
void OnGUI()
{
Vector3 localPosition = new Vector3(-Screen.width / 2 + 250, Screen.height / 2 - 25, 0);
serialPortDropDown.transform.SetLocalPositionAndRotation(localPosition, serialPortDropDown.transform.rotation);
}
}