如何存储GREEN前的newPrevious值以实现正确行列布局
Fixing Big Road Layout Logic by Ignoring Green (T) Entries
Got it, let's sort out this layout issue! The core problem is that your current logic uses the most recent entry (including Green/T) to decide whether to start a new row, but you need to ignore T entries and compare against the last valid non-T value instead. Here's how to adjust your code to make this work:
Step 1: Add a Variable to Track the Last Valid Non-T Value
First, add a new variable alongside your existing ones to store the last meaningful color (P/B) before any T entries:
string[,] table = new string[104, 15]; int xIndex = -1; int yIndex = 0; string newPrevious = "placeholder"; // New variable to track the last non-T value string actualPrevious = "placeholder"; //P = BLUE, B = RED, T = GREEN string[] strData = {"P ,B ,P ,P B,B ,P ,B ,T ,P ,P "}; string[] scoreBoard = new string[] {"P ", "B ", "T ", "P B","B P" }; string OriginalData = "";
Step 2: Update the Layout Logic to Use the Valid Previous Value
Modify your Win_Log coroutine to update actualPrevious only when dealing with non-T entries, and use this variable for your row/column decision logic:
IEnumerator Win_Log(){ yield return new WaitForEndOfFrame (); for (int i = 0; i < strData.Length; i++) { OriginalData += strData [i]; OriginalData += ","; } string[] newNewData = OriginalData.Split (','); string result = ""; foreach (string newStrData in newNewData) { Debug.Log ("This is the data : " + newStrData); GameObject o = Instantiate (prefab_gameobject) as GameObject; o.transform.SetParent (pos_big_road); o.transform.localScale = Vector3.one; img = (RawImage)o.GetComponent<RawImage> (); //check the length so that it won't throw an exception if (newStrData.Length > 1) { //get only the first letter of the value P,B,T result = newStrData.Substring (0, 1); } else { result = ""; } #region BIG ROAD if(table.GetLength(0) <= xIndex){ break; } // Update actualPrevious only if current result is not T if (!result.Equals("T")) { actualPrevious = result; } // Use actualPrevious (last non-T value) to decide layout if ((result.Equals(actualPrevious) || result.Equals("T")) && yIndex < table.GetLength(1)) { yIndex += 1; table [xIndex, yIndex] = result; } else { xIndex += 1; yIndex = 0; table [xIndex, yIndex] = result; } newPrevious = result ; o.transform.localPosition = new Vector3 (xIndex * 93, yIndex * -93, 0f); #endregion } }
Key Changes Explained
actualPreviousVariable: This stores the last non-T color (P/B) so we always compare against a meaningful value, not the Green/T entries.- Conditional Update for
actualPrevious: We only update this variable when the current result is P or B. T entries leave it unchanged, so subsequent entries reference the last valid color before the T. - Adjusted Layout Condition: The row/column check now uses
actualPreviousinstead ofnewPrevious(except for handling T's placement in the same column). This ensures that after a T, the next color compares to the one before the T, triggering a new row when needed.
Test Case Validation
- For your first test scenario (
P ,B ,P ,P B,B ,P ,B ,T ,P ,P), the final two P's will compare against the B before the T, so they'll correctly start a new row instead of continuing in the same column. - For your second test scenario (
P ,B ,P ,P B,B ,P ,P ,T ,B ,B), the final two B's compare against the P before the T, so they'll also start a new row as expected.
内容的提问来源于stack exchange,提问作者Ginxxx
相关产品推荐
相关产品推荐

