【问题标题】:Item in ScrollView is not seen in Scene, but it shows in hierarchyScrollView 中的项目在场景中看不到,但它显示在层次结构中
【发布时间】:2020-12-29 22:27:14
【问题描述】:

我正在尝试显示一个动态生成的列表。我创建了一个包含我需要的东西的预制件(一个 TextView、3 个 TMP_InputFields 和 2 个按钮。)

为了管理不同的列表项,我创建了一个脚本(SkillManager,因为这些项代表玩家可以选择的技能),我将其附加到预制件中。

然后,我将每个项目(目前我只添加一个用于测试目的)添加到列表中,迭代该列表,并将预制添加到 ScrollView 的内容:

for(int i = 0; i < listaSkills.Count; i++)
    {
        GameObject listItem = Instantiate(SkillPrefab) as GameObject;
        listItem.GetComponent<SkillManager>().skill = listaSkills[i];
        //listItem.transform.SetParent(SkillsContent.transform, false);
        listItem.transform.parent = SkillsContent.transform;
    }

当我运行它时,在 ScrollView 中看不到任何项目,但我可以看到 SkillItem 添加到层次结构中:

如果我在播放后移动到场景选项卡,我会看到一个带有红线的正方形:

为什么我的商品没有显示?为什么是红十字?如何填充我的 ScrollView?

编辑:

这是 SkillManager 的代码,添加到 SkillPrefab 的脚本:

public class SkillManager : MonoBehaviour
{
public TMP_InputField toSpend;
public TMP_InputField rangos;
public TMP_InputField modificadores;
public TMP_InputField total;
public Button plusButton;
public Button minusButton;
public TMP_Text nombre;
public Skill skill;
private int modificador;
private int pointsToSpend;
private int totalPoints;

// Start is called before the first frame update
void Start()
{
    print("Start");
    if(total!=null)
    total.text = "0";
    if(modificadores!=null)
    modificadores.text = "0";
    if (toSpend != null)
    {
        toSpend.GetComponent<TMP_InputField>().text = GetSkillPoints();
        totalPoints = int.Parse(total.GetComponent<TMP_InputField>().text);
        pointsToSpend = int.Parse(toSpend.GetComponent<TMP_InputField>().text);
    }
    else
    {
        GameObject GameObjectToSpend = GameObject.FindGameObjectWithTag("tospend");
        toSpend = GameObjectToSpend.GetComponent<TMP_InputField>();
        if (toSpend == null)
        {
            print("Sigue siendo nulo");
        }
        else
        {
            toSpend.text= GetSkillPoints();

            //totalPoints = int.Parse(total.GetComponent<TMP_InputField>().text);
            if(total!=null)
            totalPoints = int.Parse(total.text);
            if(toSpend!=null)
            pointsToSpend = int.Parse(toSpend.text);
        }
    }
    if (skill != null)
    {
        modificador = GetModificador(skill);
        string sModificador = modificadores.GetComponent<TMP_InputField>().text;
        int iModificador = int.Parse(sModificador);
        modificadores.GetComponent<TMP_InputField>().text = iModificador.ToString();
    }

    if (plusButton != null)
    {
        plusButton.onClick.AddListener(PlusButtonClicked);
        minusButton.onClick.AddListener(MinusButtonClicked);
    }
    
}

private string GetSkillPoints()
{
    return "1";
}

public void MinusButtonClicked()
{
    string ranks = rangos.GetComponent<TMP_InputField>().text;
    int ranksInt = int.Parse(ranks);
    if (ranksInt > 0)
    {
        int newRank = ranksInt - 1;
        pointsToSpend += 1;
        rangos.GetComponent<TMP_InputField>().text = newRank.ToString();
        toSpend.GetComponent<TMP_InputField>().text = pointsToSpend.ToString();
        total.GetComponent<TMP_InputField>().text = (newRank + modificador).ToString();
        skill.Puntos = newRank;
    }
}

public void PlusButtonClicked()
{
    
    string ranks=rangos.GetComponent<TMP_InputField>().text;
    int ranksInt = int.Parse(ranks);
    Character character = Almacen.instance.Character;
    int level = character.CharacterLevel;
    if (ranksInt < level && pointsToSpend > 0)
    {
        int newRank = ranksInt + 1;
        rangos.GetComponent<TMP_InputField>().text = newRank.ToString();
        pointsToSpend -= 1;
        toSpend.GetComponent<TMP_InputField>().text = pointsToSpend.ToString();
        total.GetComponent<TMP_InputField>().text = (newRank + modificador).ToString();
        skill.Puntos = newRank;
    }
}

private int GetModificador(Skill skill)
{
    int retorno=0;
    if (skill.Clasea)
    {
        retorno += 3;
    }
    else
    {
        retorno += 0;
    }
    retorno += GetModificadorCaracteristica();
    return retorno;
}

private int GetModificadorCaracteristica()
{
    Utils utils = new Utils();
    int retorno = 0;
    int characteristic=0;
    switch (skill.Caracteristica)
    {
        case "Fue":
            characteristic = Almacen.instance.Character.EffectiveStr;
            break;
        case "Des":
            characteristic = Almacen.instance.Character.EffectiveDex;
            break;
        case "Con":
            characteristic = Almacen.instance.Character.EffectiveCon;
            break;
        case "Int":
            characteristic = Almacen.instance.Character.EffectiveInt;
            break;
        case "Sab":
            characteristic = Almacen.instance.Character.EffectiveWis;
            break;
        case "Car":
            characteristic = Almacen.instance.Character.EffectiveCha;
            break;

    }
    retorno = utils.GetCharModifier(characteristic);
    return retorno;
}
}

【问题讨论】:

  • 你的元素有负数(红叉表示)
  • 但它没有负数……你可以在截图中看到。
  • 我可以看到它确实如此......正如红十字所说的......你已经将它设置为从左边开始 1700 像素......也许这太过分了?
  • 你把我放在了正确的道路上......通过执行 'listItem.transform.localScale = new Vector3(1, 1, 1); listItem.transform.localPosition = new Vector3(0, 0, 0);'我的项目出现在 ScrollView 中。谢谢!

标签: unity3d user-interface


【解决方案1】:

看起来您将对象实例化为 GameObject。但这不会在画布中看到,因为它不是 UI 组件。您可能希望向组件添加精灵或图像并将其实例化到 Canvas 中。它看起来像这样:

public class SkillPrefab
{
    //put all your variables here!!!

    public Sprite skillSprite;
}


public class YourClassName : MonoBehaviour
{
    [SerializeField]
    public List<SkillPrefab> skills = new List<SkillPrefab>();

    private void Update()
    {

        Sprite listItem = Instantiate(skills[0].skillSprite);  //the index is the skill you want to spawn in the list.
    }
}

这确实考虑到您已将您的技能列入您可以获取的技能列表。

【讨论】:

  • 感谢您的帮助,但我想我并不完全了解您。我的 SkillPrefab 是一个面板,里面有东西(文本、两个按钮和三个 TMP_InputField),由于设计原因,我不需要也不想要其中的 Sprite。如果我没记错的话,您上面提供的代码将在 ScrollView 中仅显示 Sprite,而不是包含我需要的东西的组件。无法在 ScrollView 中显示一组面板?
  • 你能把你的技能预制代码放在这里吗?我也不认为我理解你的问题,你能澄清一下吗,因为如果你想显示组件的属性,你可以通过访问它们来做到这一点。
  • 关于澄清,我有一个技能列表(角色可以分配点的技能)。我希望在每个技能的 ScrollView 中显示一个 SkillPrefav。 SkillPrefab 有只影响相应技能的按钮。因此,如果一项技能是“跳跃”,如果我单击加号按钮,该技能(并且只有该技能)的值应该增加 1 点。此外,还有一些其他因素会影响技能的总价值;如果一个角色有很高的敏捷,它会获得奖励。这就是“Modificadores”TMP_InputValue 的原因。如果你玩过 D&D,你应该会觉得很熟悉。
  • 绝对没有区别..通过哪种类型实例化预制件并不重要(无论哪种方式,它将作为游戏对象生成到场景中)。将对象生成到哪个父级才重要。在屏幕截图中,您可以看到该问题与此无关
  • 您的问题是您想在 UI/Canvas 中显示一个游戏对象。这就是为什么我说你需要在画布中获得一个精灵来代表技能
猜你喜欢
  • 2021-06-10
  • 1970-01-01
  • 2017-07-15
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多