简体   繁体   English

IOS中奇怪的统一错误

[英]Strange unity bug in IOS

I am suffering from a strange bug in my project. 我在项目中遇到一个奇怪的错误。 It runs in unity perfectly but when I build it for IOS, it does one thing in particular differently. 它可以完美地统一运行,但是当我为IOS构建它时,它做的事情特别不同。 When a user picks up 8 items, I have an input box display these items and what order they selected them in. This works perfectly in unity but doesn't in IOS. 当用户拿起8个项目时,我有一个输入框显示这些项目以及他们选择的顺序。这完全可以很好地协同工作,但不适用于IOS。 Anyone have any suggestions as to why this might be the case. 任何人都对为什么会这样有任何建议。 Below are the two appropriate scripts. 以下是两个合适的脚本。

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

public class PickUpItem : MonoBehaviour, IInteractable
{

    //Go back to older version of clavem, find out what broke. 

    public string DisplaySprite;

    public string DisplayImage;

    public static string ItemChoosenOrder;

    public static int counter;

    public static GameObject InventorySlots;

    public static GameObject[] PlayerItems = new GameObject[8];

    public void Interact(DisplayImage currentDisplay)
    {
        ItemPickUp();
    }

    void Start()
    {
        PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");
    }

    void Update()
    {

    }

    public void ItemPickUp()
    {
        InventorySlots = GameObject.Find("Slots");

        counter = 0;

        foreach (Transform slot in InventorySlots.transform)
        {
            if (slot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
            {
                slot.transform.GetChild(0).GetComponent<Image>().sprite =
                    Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
                Destroy(gameObject);
                break;
            }


            if (counter <= 7)
            {

                counter++;

                if (counter >= 7)
                {

                    {

                        Debug.Log("You have choosen all your items.");

                        foreach (Transform finalslot in InventorySlots.transform)
                        {
                            if (finalslot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
                            {
                                finalslot.transform.GetChild(0).GetComponent<Image>().sprite =
                                    Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
                                Destroy(gameObject);
                                break;
                            }
                        }

                        //https://hub.packtpub.com/arrays-lists-dictionaries-unity-3d-game-development/

                        if (PlayerItems[7].GetComponent<Image>().sprite.name != "empty_item")
                        {
                            PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");


                            ItemChoosenOrder = "1: " + PlayerItems[0].GetComponent<Image>().sprite.name + " 2: " + PlayerItems[1].GetComponent<Image>().sprite.name
                            + " 3: " + PlayerItems[2].GetComponent<Image>().sprite.name + " 4: " + PlayerItems[3].GetComponent<Image>().sprite.name
                                + " 5: " + PlayerItems[4].GetComponent<Image>().sprite.name + " 6: " + PlayerItems[5].GetComponent<Image>().sprite.name
                            + " 7: " + PlayerItems[6].GetComponent<Image>().sprite.name + " 8: " + PlayerItems[7].GetComponent<Image>().sprite.name;

                            Debug.Log(ItemChoosenOrder);
                            Debug.Log(counter);
                        }

                        else
                        {
                            Debug.Log("Error choosing items");
                        }


                    }


                }


            }


        }

    }
}

saveitemhash saveitemhash

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

public class SaveItemHash : MonoBehaviour
{
    public InputField choosenitems;

    public Text playerDisplay;

    public Text InfoText;

    public Button SaveButton;

    private void Awake()
    {
        if (DBManager.email == null)
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene(0);
        }
        playerDisplay.text = "User: " + DBManager.email;
    }

    public void CallAddItems()
    {
        StartCoroutine(AddChoosenItems());
    }

    IEnumerator AddChoosenItems()
    {
        //Using a unity web request to send email and choosen items string to my php scripts which echo a number depending on whether the user is successful or not.
        WWWForm form = new WWWForm();
        form.AddField("email", DBManager.email);
        form.AddField("items", choosenitems.text);
        WWW www = new WWW("https://clavem.000webhostapp.com/userregister.php", form);
        yield return www;
        if (www.text[0] == '0')
        {
            Debug.Log("User added successfully.");
            UnityEngine.SceneManagement.SceneManager.LoadScene(0);
            PickUpItem.counter = 0;
            PickUpItem.ItemChoosenOrder = " ";
        }
        else
        {
            Debug.Log("User adding failed. Error #" + www.text);
            InfoText.text = www.text;
            PickUpItem.counter = 0;
            PickUpItem.ItemChoosenOrder = " ";
        }
    }

    public void Update()
    {
        playerDisplay.text = "User: " + DBManager.email;
        if (PickUpItem.counter > 7)
        {
            //Do this for add field in last scene where the passwords are. 
            GameObject.Find("SaveButton").transform.localScale = new Vector3(1, 1, 1);
            choosenitems.text = PickUpItem.ItemChoosenOrder;
            GameObject.Find("InputField").transform.localScale = new Vector3(1, 1, 1);
            //choosenitems.text = PickUpItem.ItemChoosenOrder;
            //Debug.Log(PickUpItem.ItemChoosenOrder);
        }
        if (PickUpItem.counter < 7)
        {
            GameObject.Find("SaveButton").transform.localScale = new Vector3(0, 0, 0);
            GameObject.Find("InputField").transform.localScale = new Vector3(0, 0, 0);
        }
    }

    public void RestartScene()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene(3); ;
        PickUpItem.counter = 0;
        PickUpItem.ItemChoosenOrder = " ";
    }

}

Working answer using a List collection and directly linking the order by adding each item to the array corresponding to each inventory slot in the appropriate order. 使用List集合的有效答案,并通过将每个项目以适当的顺序添加到与每个广告位对应的数组来直接链接该订单。

PickUpItem Script PickUpItem脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;

public class PickUpItem : MonoBehaviour, IInteractable
{

    public string DisplaySprite;

    public string DisplayImage;

    public static List<string> itemOrder = new List<string>();

    public static string ItemChoosenOrder;

    public static int counter;

    public static GameObject InventorySlots;

    public static GameObject[] PlayerItems = new GameObject[8];

    public void Interact(DisplayImage currentDisplay)
    {
        ItemPickUp();
    }

    void Start()
    {
        PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");
    }

    void Update()
    {

    }

    public void ItemPickUp()
    {
        InventorySlots = GameObject.Find("Slots");

        counter = 0;

        int j;

        foreach (Transform slot in InventorySlots.transform)
        {
            if (slot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
            {
                slot.transform.GetChild(0).GetComponent<Image>().sprite =
                    Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
                Destroy(gameObject);
                break;
            }


            if (counter <= 7)
            {
                //itemOrder.Add(PlayerItems[counter].GetComponent<Image>().sprite.name);

                counter++;


                if (counter >= 7)
                {

                    {
                        PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");

                        Debug.Log("You have choosen all your items.");

                        foreach (Transform finalslot in InventorySlots.transform)
                        {
                            if (finalslot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
                            {
                                finalslot.transform.GetChild(0).GetComponent<Image>().sprite =
                                    Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
                                Destroy(gameObject);
                                break;
                            }
                        }

                        itemOrder.Add(GameObject.Find("item0").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item1").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item2").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item3").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item4").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item5").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item6").GetComponent<Image>().sprite.name);
                        itemOrder.Add(GameObject.Find("item7").GetComponent<Image>().sprite.name);


                        //https://hub.packtpub.com/arrays-lists-dictionaries-unity-3d-game-development/

                        if (PlayerItems[7].GetComponent<Image>().sprite.name != "empty_item")
                        {

                            PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");

                            for (j = 0; j < 8; j++)
                            {
                                Debug.LogFormat("Item[{0}] = {1}", j, itemOrder[j]);
                            }

                            ItemChoosenOrder = "1: " + itemOrder[0] + " 2: " + itemOrder[1]
                            + " 3: " + itemOrder[2] + " 4: " + itemOrder[3]
                                + " 5: " + itemOrder[4] + " 6: " + itemOrder[5]
                            + " 7: " + itemOrder[6] + " 8: " + itemOrder[7];

                            Debug.Log(ItemChoosenOrder);
                        }

                        else
                        {
                            Debug.Log("Error choosing items");
                        }


                    }


                }


            }


        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM