【问题标题】:Need to add image in the Instantiated object需要在实例化对象中添加图片
【发布时间】:2019-02-17 05:07:42
【问题描述】:

我正在关注 youtube 上的教程。这是一个滑动菜单教程。 https://www.youtube.com/watch?v=njfc_QYKdio&t=229s 我尝试并理解了他的所作所为。

public class SecMenuScene : MonoBehaviour {
    [Range(1, 50)]
    [Header("Controllers")]
    public int panCount;
    [Range(0, 500)]
    public int panOffset;
    [Range(0,20)]
    public int snapSpeed;
    [Range(0,6)]
    public int scaleOffset;
    [Range(0,20)]
    public int scaleSpeed;
    [Header ("Other object")]
    public GameObject panPrefab;
    public ScrollRect scrollRect;


    private GameObject[] instPans;
    private Vector2[] panPos,panScale;


    private RectTransform contentRect;
    private Vector2 contentVector;

    public int selectedPanID;
    public bool isScroling;
    // Use this for initialization
    void Start () {


        contentRect = GetComponent<RectTransform>();
        instPans = new GameObject[panCount];
        panPos = new Vector2[panCount];
        panScale = new Vector2[panCount];
        for (int i = 0; i < panCount; i++)
        {

            instPans[i] = Instantiate(panPrefab, transform, false) ;


            if (i == 0) continue;
            instPans[i].transform.localPosition = new Vector2(instPans[i - 1].transform.localPosition.x + panPrefab.GetComponent<RectTransform>().sizeDelta.x + panOffset,
                instPans[i].transform.localPosition.y);
            panPos[i] = -instPans[i].transform.localPosition;

        }
    }

    // Update is called once per frame
    void FixedUpdate () {
        if(contentRect.anchoredPosition.x>= panPos[0].x && !isScroling || contentRect.anchoredPosition.x <= panPos[panPos.Length - 1].x  && !isScroling)
        {
            scrollRect.inertia = false;
        }
        float nearestpos = float.MaxValue;
        for (int i = 0; i < panCount; i++)
        {
            float distance =Mathf.Abs( contentRect.anchoredPosition.x - panPos[i].x);
            if (distance < nearestpos)
            {
                nearestpos = distance;
                selectedPanID = i;
            }
            float scale = Mathf.Clamp(1 / (distance / panOffset) * scaleOffset, 0.5f, 1);
            panScale[i].x = Mathf.SmoothStep(instPans[i].transform.localScale.x+0.3f, scale, scaleSpeed * Time.fixedDeltaTime);
            panScale[i].y = Mathf.SmoothStep(instPans[i].transform.localScale.y+0.3f, scale, scaleSpeed * Time.fixedDeltaTime);
            instPans[i].transform.localScale = panScale[i];
        }
        float scrolVelocity = Mathf.Abs(scrollRect.velocity.x);
        if (scrolVelocity < 400 && !isScroling) scrollRect.inertia = false;

        if (isScroling || scrolVelocity>400) return;
        contentVector.x = Mathf.SmoothStep(contentRect.anchoredPosition.x, panPos[selectedPanID].x, snapSpeed * Time.fixedDeltaTime);
        contentRect.anchoredPosition = contentVector;
    }
    public void Scrolling(bool Scroll)
    {
        isScroling = Scroll;
        if (Scroll) scrollRect.inertia = true;
    }
}

但问题是现在我无法更改预制 obj 的图像。我尝试使用 SpriteRenderer 实例化 obj 并更改精灵。但无法找到解决方案。如果有人可以帮助或建议我应该阅读什么以使其正常工作。 感谢您的帮助。

【问题讨论】:

    标签: unity3d sprite game-physics 2d-games


    【解决方案1】:

    首先,您可以像声明的其他公共变量一样持有对所需精灵的引用:

     [Header ("Other object")]
    public GameObject panPrefab;
    public ScrollRect scrollRect;
    [SerializeField]
    private Sprite sprite; // or Sprite[] sprites if you need more than one
    

    然后您可以在编辑器中将图像组件添加到您的预制件中,并按照您喜欢的方式设置其大小和位置。

    for (int i = 0; i < panCount; i++)
        {
    
            instPans[i] = Instantiate(panPrefab, transform, false) ;
            // you can get the reference to your Image component and set its sprite
            Image im = instPans[i].GetComponent<Image>();
            im.sprite = sprite; // or im.sprite = sprites[i] if you are using multiple sprites
    
            if (i == 0) continue;
            instPans[i].transform.localPosition = new Vector2(instPans[i - 1].transform.localPosition.x + panPrefab.GetComponent<RectTransform>().sizeDelta.x + panOffset,
                instPans[i].transform.localPosition.y);
            panPos[i] = -instPans[i].transform.localPosition;
    
        }
    

    【讨论】:

    • Mahdad baghani 兄弟,非常感谢。感谢您的帮助。:)。还有一件事我是新手,我刚刚从视频中复制了代码。因为这个现在我知道Range,Header的使用,因为你现在序列化。但我无法理解脚本。你能评论一下这一行的代码吗?这样我就可以理解代码了。如果你有时间。如果不是那么没问题,你已经帮了我很多。谢谢
    • 感谢 Mahdad Baghani 的帮助。在你的帮助下,我能够制作我的第三场比赛。我在描述中提到了你的名字,希望你不介意。 lifeisjourney.itch.io/snowman
    【解决方案2】:

    看起来这些是 UI 对象,其中有一个 Image 组件代替了 SpriteRenderer。您需要获取此组件来更改图像源。

    例如

    instPans[i].GetComponent<Image>().sprite = mySprite;
    

    【讨论】:

    • 黑麦苔藓。兄弟,非常感谢。感谢您的帮助。:)。现在真的很管用,我有一个滑动菜单屏幕 :)
    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 2015-07-29
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2021-12-22
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多