簡體   English   中英

平面字符選擇器統一

[英]Plane character selector unity

在過去的兩天里,我一直在如何使用guldens的“購買”按鈕選擇角色,切換工作正常,但此屏幕截圖中的角色有問題。

我想當我向右或向左切換時處於相同位置,並且我嘗試了很多方法來解決此問題,直到現在仍然沒有結果

[1]: https//i.stack.imgur.com/cB2Rl.jpg

當我左右切換時,它看起來像這樣

[2]: https//i.stack.imgur.com/FPRYJ.jpg

這是我的代碼

那就是MenuScene scrpit

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MenuScene : MonoBehaviour {

    private CanvasGroup fadeGroup;
    private float fadeInSpeed = 0.33f;

    public RectTransform menuContainer;
    public Transform levelPanel;
    public Transform colorPanel;
    public Transform trailPanel;

    public Button tiltControlButton; // Change the name of the button
    public Color tiltControlEnabled;
    public Color tiltControlDisabled;

    public Text buyColorText;
    public Text buyTrailText;
    public Text goldText;

    private MenuCamera menuCam;

    private int[] colorCost = new int[] { 0, 5, 5, 5, 10, 10, 10, 15, 15, 10, 0, 5, 5, 5, 10, 10};
    private int[] trailCost = new int[] { 0, 20, 30, 40, 50, 60, 70, 80, 90, 100, 5, 5, 5, 10, 10 };
    private int selectedColorIndex;
    private int selectedTrailIndex;
    private int activeColorIndex;
    private int activeTrailIndex;

    private Vector3 desiredMenuPosition;

    private GameObject currentTrail;

    public AnimationCurve enteringLevelZoomCurve;
    private bool isEnteringLevel = false;
    private float zoomDuration = 3.0f;
    private float zoomTransition;


  private void Start()
    {

        // Check if we have an accelerometer
        if(SystemInfo.supportsAccelerometer)
        {
            // It is currently enabled ?
            tiltControlButton.GetComponent<Image>().color = (SaveManager.Instance.state.usingAccelerometer) ? tiltControlEnabled : tiltControlDisabled;
        }
        else
        {
            tiltControlButton.gameObject.SetActive(false);
        }

        //Find the only MenuCamera and assign it
        menuCam = FindObjectOfType<MenuCamera>();


        // Position our camera on the focused menu
        SetCameraTo(Manager.Instance.menuFocus);

        // Tell our gold text how much he should displaying 
        UpdateGoldText();
        //Grab the only canvasGroup in the scene 
        fadeGroup = FindObjectOfType<CanvasGroup>();

        //Start with a white screen 
        fadeGroup.alpha = 1;

        // Add button on-click events to shop buttons
        InItShop();

        //Add button on-Click events to levels 
        InitLevel();

        // Set players prefrences ( color & trail )
        OnColorSelect(SaveManager.Instance.state.activeColor);
        SetColor(SaveManager.Instance.state.activeColor);

        OnTrailSelect(SaveManager.Instance.state.activeTrail);
        SetTrail(SaveManager.Instance.state.activeTrail);

        // Make the buttons bigger for the selected items
        colorPanel.GetChild(SaveManager.Instance.state.activeColor).GetComponent<RectTransform>().localScale = Vector3.one * 1.125f;
        trailPanel.GetChild(SaveManager.Instance.state.activeTrail).GetComponent<RectTransform>().localScale = Vector3.one * 1.125f;

        // Create the trail preview 
        /*lastpreviewObject = GameObject.Instantiate(Manager.Instance.playertrails[SaveManager.Instance.state.activeTrail]) as GameObject;
        lastpreviewObject.transform.SetParent(trailPreviewObject);
        lastpreviewObject.transform.localPosition = Vector3.zero;*/

    }

    private void Update()
    {
        // Fade-in
        fadeGroup.alpha = 1 - Time.timeSinceLevelLoad * fadeInSpeed;

        //Menu navigation
        menuContainer.anchoredPosition3D = Vector3.Lerp(menuContainer.anchoredPosition3D, desiredMenuPosition, 0.1f);

        // Entering level zoom
        if(isEnteringLevel)
        {
            // Add to the zoomTransition float 
            zoomTransition += (1 / zoomDuration) * Time.deltaTime;

            // Change the scale, following the animation curve 
            menuContainer.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 5, enteringLevelZoomCurve.Evaluate(zoomTransition));

            // Change the desired position of the canvas , so it can follow the scale up
            // This zooms in the center 
            Vector3 newDesiredPosition = desiredMenuPosition * 5;
            // This adds to the specific position of the level on the canvas 
            RectTransform rt = levelPanel.GetChild(Manager.Instance.currentLevel).GetComponent<RectTransform>();
            newDesiredPosition -= rt.anchoredPosition3D * 5;

            // This line will override the previous position
            menuContainer.anchoredPosition3D = Vector3.Lerp(desiredMenuPosition, newDesiredPosition, enteringLevelZoomCurve.Evaluate(zoomTransition));

            // Fade to white screen , this will override the first line in the update 
            fadeGroup.alpha = zoomTransition;

            // Are we done with the animation 
            if(zoomTransition >= 1)
            {
                // Enter the level 
                SceneManager.LoadScene("Game");
            }
        }

    } 

    private void InItShop()
    {
        // Just make sure we've assigned the referencess
        if (colorPanel == null || trailPanel == null)
            Debug.Log("You didnt not asign the color/trail panel in the inspector");

        //For every children transfrom under our color panel , find the button and add onClick
        int i = 0;
        foreach (Transform t in colorPanel)
        {
            int currentIndex = i;

            Button b = t.GetComponent<Button>();
            b.onClick.AddListener(() => OnColorSelect(currentIndex));

            // Set color of the image, based on if owned or not
            Image img = t.GetComponent<Image>();
            img.color = SaveManager.Instance.IsTrailOwned(i)
                ? Manager.Instance.playerColors[currentIndex]
                : Color.Lerp(Manager.Instance.playerColors[currentIndex], new Color(0, 0, 0, 1), 0.25f);

            i++;
        }
        //Reset index 
        i = 0;
        //Do the same for the trail 
        foreach (Transform t in trailPanel)
        {
            int currentIndex = i;

            Button b = t.GetComponent<Button>();
            b.onClick.AddListener(() => OnTrailSelect(currentIndex));

            // Set trail of the image, based on if owned or not
            RawImage img = t.GetComponent<RawImage>();
            img.color = SaveManager.Instance.IsColorOwned(i) ? Color.white : new Color (0.7f, 0.7f, 0.7f);

            i++;
        }

        // Set the previous trail , to prevent bug when spawing later 
       // previousTrail = trailPanel.GetChild(SaveManager.Instance.state.activeTrail).GetComponent<RawImage>().texture;
    }

    private void InitLevel()
    {
        // Just make sure we've assigned the referencess
        if (levelPanel == null )
            Debug.Log("You didnt not asign the level panel in the inspector");

        //For every children transfrom under our level panel , find the button and add onClick
        int i = 0;
        foreach (Transform t in levelPanel)
        {
            int currentIndex = i;

            Button b = t.GetComponent<Button>();
            b.onClick.AddListener(() => OnLevelSelect(currentIndex));

            Image img = t.GetComponent<Image>();

            // Is it unlocked ?
            if (i <= SaveManager.Instance.state.completedLevel)
            {
                //It is unlocked!
                if(i == SaveManager.Instance.state.completedLevel)
                {
                    //It's not completed !
                    img.color = Color.white;
                }
                else
                {
                    // level is already completed !
                    img.color = Color.green;
                }
            }
            else
            {
                // Level isn't unlock , disable the button
                b.interactable = false;

                // Set to a dark color
                img.color = Color.grey; 
            }
            i++;
        }
    }

    private void SetCameraTo(int menuIndex)
    {
        NavigateTo(menuIndex);
        menuContainer.anchoredPosition3D = desiredMenuPosition;
    }

    private void NavigateTo(int menuIndex)
    {
        switch(menuIndex)
        {
            // 0 && default case = main menu 
            default:
            case 0:
                desiredMenuPosition = Vector3.zero;
                menuCam.BackToMainMenu();
                break;
            // 1 = play menu
            case 1:
                desiredMenuPosition = Vector3.right * 1280;
                menuCam.MoveToLevel();
                break;
            // 2= shop menu
            case 2:
                desiredMenuPosition = Vector3.left * 1280;
                menuCam.MoveToShop();
                break;
        }
    }

    private void SetColor(int index)
    {
        // Set the active index 
        activeColorIndex = index;
        SaveManager.Instance.state.activeColor = index;

        //Change the color of the player 
        Manager.Instance.playerMaterial.color = Manager.Instance.playerColors[index];

        //change buy/set button text
        buyColorText.text = "Current";

        // Remember prefrences
        SaveManager.Instance.Save();
    }

    private void SetTrail(int index)
    {
        // Set the active index
        activeTrailIndex = index;
        SaveManager.Instance.state.activeTrail = index;

        //Change the trail of the player
        if (currentTrail != null)
            Destroy(currentTrail);

        // Create the new trail
        currentTrail = Instantiate(Manager.Instance.playertrails[index]) as GameObject;

        // Set it as a children of the player
        currentTrail.transform.SetParent(FindObjectOfType<MenuPlayer>().transform);

        // Fix the wierd scalling issues / rotation issues
        currentTrail.transform.localPosition = Vector3.zero;
        currentTrail.transform.localRotation = Quaternion.Euler(0, 0, 90);
        currentTrail.transform.localScale = Vector3.one * 0.01f; 

        //change buy/set button text
        buyTrailText.text = "Current";

        // Remember prefrences
        SaveManager.Instance.Save();
    }

    private void UpdateGoldText()
    {
        goldText.text = SaveManager.Instance.state.gold.ToString();
    }

    //Buttons
    public void OnPlayClick()
    {
        NavigateTo(1);
        Debug.Log("Play Button Has Been Clicked");
    }

    public void OnShopClick()
    {
        NavigateTo(2);
        Debug.Log("Your in the shop");
    }

    public void OnBackClick()
    {
        NavigateTo(0);
        Debug.Log("Back button has been clicked");
    }

    private void OnTrailSelect(int currentIndex)
    {
        Debug.Log("Selecting trail button : " + currentIndex);

        // If the button clicked is already selected, exist 
        if (selectedTrailIndex == currentIndex)
            return;

        // Preview trail
        // Get the image of the preview button
        //trailPanel.GetChild(selectedTrailIndex).GetComponent<RawImage>().texture = previousTrail;
        // Keep the new trails preview image in the previous trail
        //previousTrail = trailPanel.GetChild(currentIndex).GetComponent<RawImage>().texture;
        // Set the new trail preview image to the other camera
        //trailPanel.GetChild(currentIndex).GetComponent<RawImage>().texture = trailPreviewTexture;

        //  Change the physical object of the trail preview
       /* if (lastpreviewObject != null)
            Destroy(lastpreviewObject);
        lastpreviewObject = GameObject.Instantiate(Manager.Instance.playertrails[currentIndex]) as GameObject;
        lastpreviewObject.transform.SetParent(trailPreviewObject);
        lastpreviewObject.transform.localPosition = Vector3.zero;*/

        // Make the icon slightly bigger 
        trailPanel.GetChild(currentIndex).GetComponent<RectTransform>().localScale = Vector3.one * 1.125f;
        // Put the previuos one on normal scale
        trailPanel.GetChild(selectedTrailIndex).GetComponent<RectTransform>().localScale = Vector3.one;


        // Set the selected color 
        selectedTrailIndex = currentIndex;

        //Change the content of the buy/set button button , depending on the state of the color 
        if (SaveManager.Instance.IsTrailOwned(currentIndex))
        {
            // Trail is owned 
            // Is it already our current color ?
            if (activeTrailIndex == currentIndex)
            {
                buyTrailText.text = "Current";
            }
            else
            {
                buyTrailText.text = "Select";
            }
        }
        else
        {
            // Trail isnt owned
            buyTrailText.text = "Buy:" + trailCost[currentIndex].ToString();
        }
    }

    private void OnColorSelect(int currentIndex)
    {
        Debug.Log("Selecting Color button : " + currentIndex);

        // If the button clicked is already selected, exist 
        if (selectedColorIndex == currentIndex)
            return;

        // Make the icon slightly bigger 
        colorPanel.GetChild(currentIndex).GetComponent<RectTransform>().localScale = Vector3.one * 1.125f;
        // Put the previuos one on normal scale
        colorPanel.GetChild(selectedColorIndex).GetComponent<RectTransform>().localScale = Vector3.one;

        // Set the selected color 
        selectedColorIndex = currentIndex;

        //Change the content of the buy/set button button , depending on the state of the color 
        if(SaveManager.Instance.IsColorOwned(currentIndex))
        {
            // color is owned
            // Is it already our current color ?
            if(activeColorIndex == currentIndex)
            {
                buyColorText.text = "Current";
            }
            else
            {
                buyColorText.text = "Select";
            }

        }
        else
        {
            // Color isnt owned
            buyColorText.text = "Buy: " + colorCost[currentIndex].ToString();
        }
    }

    private void OnLevelSelect(int currentIndex)
    {
        Manager.Instance.currentLevel = currentIndex;
        isEnteringLevel = true;
        Debug.Log("Selecting level : " + currentIndex);
    }

    public void OnColorBuy()
    {
        Debug.Log("Buy color");

        // Is the selected color owned
        if(SaveManager.Instance.IsColorOwned(selectedColorIndex))
        {
            // Set the color !
            SetColor(selectedColorIndex);
        }
        else
        {
            // Attempt to buy the color 
            if (SaveManager.Instance.BuyColor(selectedColorIndex, colorCost[selectedColorIndex]))
            {
                // Success!
                SetColor(selectedColorIndex);

                // Change the color of the button 
                colorPanel.GetChild(selectedColorIndex).GetComponent<Image>().color = Manager.Instance.playerColors[selectedColorIndex];


                //Update gold text
                UpdateGoldText();
            }
            else
            {
                // Dont have enough gold
                // Play sound feedback  
                Debug.Log("Not enough gold");
            }
        }
    }

    public void OnTrailBuy()
    {
        Debug.Log("Buy Trail");

        // Is the selected trail owned
        if (SaveManager.Instance.IsTrailOwned(selectedTrailIndex))
        {
            // Set the trail !
            SetTrail(selectedTrailIndex);
        }
        else
        {
            // Attempt to buy the trail 
            if (SaveManager.Instance.BuyTrail(selectedTrailIndex, trailCost[selectedTrailIndex]))
            {
                // Success!
                SetTrail(selectedTrailIndex);

                // Change the color of the button 
                trailPanel.GetChild(selectedTrailIndex).GetComponent<Image>().color = Color.white;

                //Update gold text
                UpdateGoldText();
            }
            else
            {
                // Dont have enough gold
                // Play sound feedback  
                Debug.Log("Not enough gold");
            }
        }
    }

    public void OnTiltControl()
    {
        // Toggle the accelerometer bool 
        SaveManager.Instance.state.usingAccelerometer = !SaveManager.Instance.state.usingAccelerometer;

        // Make sure we save the player's preferences
        SaveManager.Instance.Save();

        // Change the display image of the tilt control button
        tiltControlButton.GetComponent<Image>().color = (SaveManager.Instance.state.usingAccelerometer) ? tiltControlEnabled : tiltControlDisabled;
    }

}

平面選擇座

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlaneSelection : MonoBehaviour {

    private GameObject[] characterList;

    private int index;

    private void Start()
    {
        index = PlayerPrefs.GetInt("CharacterSelected");

        characterList = new GameObject[transform.childCount];

        // Fill the array with our models 
        for (int i = 0; i < transform.childCount; i++)
            characterList[i] = transform.GetChild(i).gameObject;

        // We toggle off their render
        foreach (GameObject go in characterList)
            go.SetActive(false);

        // We toggle on the selected character
        if (characterList[index])
            characterList[index].SetActive(true);
    }

    public void ToggleLeft()
    {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index--;//index -= 1 , index - 1;
        if (index < 0)
            index = characterList.Length - 1;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void ToggleRight()
    {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index++;//index -= 1 , index - 1;
        if (index == characterList.Length)
            index = 0;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void ConfirmButton()
    {
        PlayerPrefs.SetInt("CharacterSelected", index);
        SceneManager.LoadScene("Game");
    }
}

菜單相機腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MenuCamera : MonoBehaviour {

    private Vector3 startPosition;
    private Quaternion startRotation;

    private Vector3 desiredPosition;
    private Quaternion desiredRotation;

    public Transform shopWaypoint;
    public Transform levelWaypoint;

    private void Start()
    {
        startPosition = desiredPosition = transform.localPosition;
        startRotation = desiredRotation = transform.localRotation;
    }

    private void Update()
    {
        float x = Manager.Instance.GetPlayerInput().x;

        transform.localPosition = Vector3.Lerp(transform.localPosition, desiredPosition + new Vector3(0, x, 0) * 0.01f, 0.1f);
        transform.localRotation = Quaternion.Lerp(transform.localRotation, desiredRotation, 0.1f);
    }

    public void BackToMainMenu()
    {
        desiredPosition = startPosition;
        desiredRotation = startRotation;
    }

    public void MoveToShop()
    {
        desiredPosition = shopWaypoint.localPosition;
        desiredRotation = shopWaypoint.localRotation;
    }

    public void MoveToLevel()
    {
        desiredPosition = levelWaypoint.localPosition;
        desiredRotation = levelWaypoint.localRotation;
    }
}

您需要檢查場景,在編輯模式下所有平面是否都位於正確的位置? 在運行游戲時打開和關閉它們,看看它們在哪里。 這些按鈕不會影響其位置,它們不是問題的原因。 我將重置對象轉換的父對象,然后將它們中的每一個放置在正確的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM