我已經開始為我的游戲開發一個早期的 alpha 選單,我想知道如何排除陣列中的專案,特別是在統一中。我正在嘗試制作除當前使用的物品之外的所有物品。我不知道我該怎么做,如果有人能幫忙,那就太棒了。
這是我當前的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class CS
{
public List<GameObject> CanvasButtons;
}
public class CanvasSwitcher : MonoBehaviour
{
public List<CS> Screen = new List<CS>();
static int switcher;
public static void ScreenSwitchPlus()
{
switcher = 1;
}
public static void ScreenSwitchMinus()
{
switcher -= 1;
}
public void Update()
{
foreach(GameObject l in Screen[switcher].CanvasButtons)
{
l.SetActive(true);
}
}
}
uj5u.com熱心網友回復:
for在這里使用回圈而不是回圈會更明智foreach:
public void Update()
{
int ignore = 2;
for(int i = 0; i < Screen[switcher].CanvasButtons.Count; i )
{
if(i != ignore)
Screen[switcher].CanvasButtons[i].SetActive(true);
else
Screen[switcher].CanvasButtons[i].SetActive(false);
}
}
if如果沒有(承認可讀性較差),它可能會更短:
public void Update()
{
int ignore = 2;
for(int i = 0; i < Screen[switcher].CanvasButtons.Count; i )
{
Screen[switcher].CanvasButtons[i].SetActive(i != ignore);
}
}
......甚至更短,使 CanvasButtons 脫離回圈:
public void Update()
{
int ignore = 2;
var collection = Screen[switcher].CanvasButtons;
for(int i = 0; i < collection.Count; i )
{
collection[i].SetActive(i != ignore);
}
}
uj5u.com熱心網友回復:
我得到了它!有一段時間我有點空想,我在第二個 for 陳述句中弄亂了一些值,但我明白了。我會在這里發布代碼。我總是忘記數學中的小事。就像我常說的,我不是解決問題,而是制造問題。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class CS
{
public List<GameObject> CanvasButtons;
}
public class CanvasSwitcher : MonoBehaviour
{
public List<CS> Screen = new List<CS>();
static int switcher;
public static void ScreenSwitchPlus()
{
switcher = 1;
}
public static void ScreenSwitchMinus()
{
switcher -= 1;
}
public void FixedUpdate()
{
List<CS> csco = Screen;
for(int i1 = 0; i1 < csco.Count;)
{
List<GameObject> collection = csco[i1].CanvasButtons;
for (int i = 0; i < collection.Count; i )
{
collection[i].SetActive(i1 == switcher);
switch(i == collection.Count - 1)
{
case true:
i1 ;
break;
}
}
}
}
}
uj5u.com熱心網友回復:
我認為你把事情弄得太復雜了。
它可能只是
public void Update()
{
for(var i = 0; i < Screen.Count; i )
{
foreach(var button in Screen[i].CanvasButtons)
{
button.SetActive(i == switcher);
}
}
}
不過還有兩點。
首先,我會確保這switcher實際上是一個有效值。您要么希望將值限制在索引范圍內,例如
public static void ScreenSwitchPlus()
{
switcher = Mathf.Clamp(switcher 1, 0, Screen.Count);
}
public static void ScreenSwitchMinus()
{
switcher = Mathf.Clamp(switcher - 1, 0, Screen.Count);
}
或者您可以根據您的需要在末端實施環繞,例如
public static void ScreenSwitchPlus()
{
switcher = (switcher 1) % Screen.Count;
}
public static void ScreenSwitchMinus()
{
switcher -= 1;
if(switcher < 0) switcher = Screen.Count - 1;
}
然后最后如前所述,我不會做這個民意調查來獲取Update 每一幀中的值,而是事件驅動。
如果你真的需要(我會聲稱這只是懶惰;))有static你可以附加到一個事件的價值和方法,比如
private static event Action<int> OnSwitcherChanged;
public static void ScreenSwitchPlus()
{
switcher = (switcher 1) % Screen.Count;
OnSwitcherChanged?.Invoke(switcher);
}
public static void ScreenSwitchMinus()
{
switcher -= 1;
if(switcher < 0) switcher = Screen.Count - 1;
OnSwitcherChanged?.Invoke(switcher);
}
然后聽那個事件
private void OnEnable()
{
OnSwitcherChanged = HandleSwitchChanged;
}
private void OnDisable()
{
OnSwitcherChanged -= HandleSwitchChanged;
}
private void HandleSwitchChanged(int newIndex)
{
for(var i = 0; i < Screen.Count; i )
{
foreach(var button in Screen[i].CanvasButtons)
{
button.SetActive(i == switcher);
}
}
}
uj5u.com熱心網友回復:
嘗試這個
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class CS {
public List<GameObject> CanvasButtons;
}
public class CanvasSwitcher : MonoBehaviour {
public List<CS> Screen = new List<CS>();
private static bool m_changed = false;
private static int m_lastSwitcher = 0;
private static int m_switcher = 0;
static int switcher {
get {
return m_switcher;
}
set {
if ( switcher != value ) {
m_lastSwitcher = m_switcher;
m_changed = true;
m_switcher = value;
}
}
}
public static void ScreenSwitchPlus() {
switcher = 1;
}
public static void ScreenSwitchMinus() {
switcher -= 1;
}
private void Update() {
if ( m_changed ) {
UpdateCS();
m_changed = false;
}
}
void UpdateCS() {
if ( m_lastSwitcher < Screen.Count ) {
var canvasBtns = Screen[ m_lastSwitcher ].CanvasButtons;
for ( int i = 0; i < canvasBtns.Count; i ) {
canvasBtns[ i ].SetActive( false );
}
}
if ( switcher < Screen.Count ) {
var canvasBtns = Screen[ switcher ].CanvasButtons;
for ( int i = 0; i < canvasBtns.Count; i ) {
canvasBtns[ i ].SetActive( true );
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/448006.html
下一篇:實體化產生數百個專案而不是1個
