我有一个选择菜单代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectionMenu : MonoBehaviour
{
public List<MenuButton> buttons = new List<MenuButton>();
public GameObject canvas;
private Vector2 Mouseposition;
private Vector2 fromVector2M = new Vector2(0.5f, 1.0f);
private Vector2 centercircle = new Vector2(0.5f, 0.5f);
private Vector2 toVector2M;
public int menuItems;
public int CurMenuItem;
private int OldMenuItem;
private bool isDone = false;
// Start is called before the first frame update
void Start()
{
canvas.SetActive(false);
menuItems = buttons.Count;
foreach (MenuButton button in buttons)
{
button.sceneImage.color = button.NormalColor;
}
CurMenuItem = 0;
}
// Update is called once per frame
void Update()
{
GetCurrentMenuItem();
if (Input.GetButtonDown("Fire1"))
ButtonAction();
if (Input.GetKey(KeyCode.Mouse1) && !isDone)
{
canvas.SetActive(true);
CrossHair.ok = true;
}
else
{
canvas.SetActive(false);
CrossHair.ok = false;
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
isDone = false;
}
}
public void GetCurrentMenuItem()
{
Mouseposition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
toVector2M = new Vector2(Mouseposition.x / Screen.width, Mouseposition.y / Screen.height);
float angle = (Mathf.Atan2(fromVector2M.y - centercircle.y, fromVector2M.x - centercircle.x) - Mathf.Atan2(toVector2M.y - centercircle.y, toVector2M.x - centercircle.x)) * Mathf.Rad2Deg;
if (angle < 0)
angle += 360;
CurMenuItem = (int)(angle / (360 / menuItems));
if(CurMenuItem != OldMenuItem)
{
buttons[OldMenuItem].sceneImage.color = buttons[OldMenuItem].NormalColor;
RectTransform oldTrans = buttons[OldMenuItem].sceneImage.gameObject.GetComponent<RectTransform>();
oldTrans.sizeDelta = new Vector3(250, 250);
Debug.Log("Resized: " + buttons[OldMenuItem].name);
OldMenuItem = CurMenuItem;
buttons[CurMenuItem].sceneImage.color = buttons[CurMenuItem].HighlightedColor;
RectTransform newTrans = buttons[CurMenuItem].sceneImage.gameObject.GetComponent<RectTransform>();
oldTrans.sizeDelta = new Vector3(280, 280);
}
}
public void ButtonAction()
{
buttons[CurMenuItem].sceneImage.color = buttons[CurMenuItem].PressedColor;
if(CurMenuItem == 0)
{
SetColor.color = buttons[CurMenuItem].HighlightedColor;
}
isDone = true;
}
}
[System.Serializable]
public class MenuButton
{
public string name;
public Image sceneImage;
public Color NormalColor = Color.white;
public Color HighlightedColor = Color.grey;
public Color PressedColor = Color.gray;
}
除一件事外,一切都按预期工作:缩放比例不按预期工作。这行:
oldTrans.sizeDelta = new Vector3(280, 280);
此行仅在我离开突出显示的选择(菜单项/按钮)后才运行 而不是当它开始突出显示时。它之所以怪异,是因为在它之前的一行我将按钮设置为突出显示,并且直到它不再突出显示之后,该行才会运行。另外,这一行:
oldTrans.sizeDelta = new Vector3(250, 250);
完全不运行。每次该行日志之后立即显示的Debug.Log。
答案 0 :(得分:2)
sizeDelta
sizeDelta
会告诉您此RectTransform相对于其父级要大或小。为了更改此设置,您必须修改锚点。如果您试图将RectTransform设置为具有基于其内容的大小,那么也许Google Cloud Natural Language API - How is document magnitude calculated?就是您所要的。