团结 - 如何在slider`s特定值实例预制

问题描述 投票:0回答:2

我真的做基于在其中,每个级别,你必须在得分跟踪杆3个关卡水平的2D游戏。玩家必须达到最低的检查站,以便能够传递到一个新的水平,但如果他在达到第二和第三检查站将获得奖金。

我认为在使用滑块作为打分吧。我的问题是:

是否有存储Slider的条的特定值在Start方法和实例在该位置标记预制的方法吗?下面是一个例子:

  • 滑块的第1级最大值是100。
  • 我想实例化所述第一标记,与在y一些填充,在滑块的50`s位置,在75位置的第二和100位置上的第三位。

我现在的逻辑是,我要,不知何故,得到我想要的价值,找到他的变换,但我可以`吨找到一种方法来编写这个,我不知道如何得到我想要的位置。

下面是一些图片来说明什么我真的想这样做:

Level 1 Image

Level 2 Image

c# unity3d slider instantiation
2个回答
1
投票

我会得到滑块的宽度属性,然后除以sliderMax,其结果将是在所述滑块上的单一的%的宽度。那么你可以添加或本subract多拿到栏上一个百分比的地方。

例如:slider.x = 50 slider.width = 200;

increment = slider.width/100; //这将导致两个,让你每增加1%的两个像素。

所以你的50%的位置将是:sliderx+(increment*50);

记住,这是所有的伪代码,旨在为您提供如何acheive你想要的结果的想法


0
投票

我找到了解决办法!

基于我能够做到这一点的见解:

    void SpawnCheckPoint() {
        mySlider.maxValue = gameLevel[currentLevel].maxValue; //Set the slider's Max Value to the max value of the level.
        float sliderMaxValue = mySlider.maxValue;
        float sliderWidth = slider.GetComponent<RectTransform>().sizeDelta.x; //Get the width of the Slider.
        float zeroValue = slider.transform.position.x - (sliderWidth / 2); //Get the leftmost corner of the slider.

        //Loop to Instantiate the 3 checkpoints.
        for (int i = 0; i < gameLevel[currentLevel].values.Length; i++) { 
            float valueToIncrement = (gameLevel[currentLevel].values[i] / sliderMaxValue); //Get the % of the checkpoint based on the max value of the level.
            float newPos = (sliderWidth * valueToIncrement); //New position in screen

            //Instantiate the object as a child of the Slider
            GameObject checkpoint = Instantiate(checkPoint, new Vector3(zeroValue + newPos - xPadding, slider.transform.position.y - yPadding, slider.transform.position.z),
                                            Quaternion.identity, GameObject.FindGameObjectWithTag("Slider").transform);
        }
    }

它可能不是做我想做什么是最好的方式,但它的工作就好了。

谢谢大家谁试图帮助我,你的见解是非常有用的。

© www.soinside.com 2019 - 2024. All rights reserved.