Форум программистов, компьютерный форум, киберфорум
Программирование Android
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
0 / 0 / 0
Регистрация: 28.01.2021
Сообщений: 4
1

Код на C# для Андроид приложения в Unity2D , как сделать масив со сцен и перенаправить их на клон префаба!

28.01.2021, 14:52. Показов 611. Ответов 0

Author24 — интернет-сервис помощи студентам
Здраствуйте! Ситуация такая! Это должно быть меню ля игры! Есть сцена с префабом и сроллом , в которой клонируется экземпляр прифаба на префабе весит код:
C#
1
2
3
4
5
6
7
8
9
10
11
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class LoadScene : MonoBehaviour
{
    public void Level_1_1_Scene()
    {
        SceneManager.LoadScene(2);
    }
 
}
на самом Scroll View>Viewport>Content весит скрипт который клонирует этот префаб 5 раз :
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class SnapScrolling : MonoBehaviour
{
    [Range(1,50)]
    [Header("Controllers")]
    public int panCount;
    [Range(0,500)]
    public int panOffset;
    [Range(0f, 20f)]
    public float snapSpeed;
    [Range(0f, 5f)]
    public float scaleOffset;
    [Range(0f, 20f)]
    public float scaleSpeed;
    [Header("Other objects")]
    public GameObject panPrefab;
 
    public Sprite[] sprites;
 
    public ScrollRect scrollRect;
 
    private GameObject[] instPans;
 
    private Vector2[] pansPos;
 
    private Vector2[] pansScale;
 
    private RectTransform contentRect;
 
    private Vector2 contentVector;
 
    private int selectedPanID;
 
    private bool isScrolling;
 
    
 
    private void Start()
    {
        contentRect = GetComponent<RectTransform>();
        instPans = new GameObject[panCount];
        pansPos = new Vector2[panCount];
        pansScale = 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) ;
            instPans[i].GetComponent<Image>().sprite = sprites[i];
            //тут нужно вставить подгрузку сцен к клонам префаба//
            pansPos[i] = -instPans[i].transform.localPosition;
            
        }
    }
    private void FixedUpdate()
    {
        if (contentRect.anchoredPosition.x >= pansPos[0].x && !isScrolling || contentRect.anchoredPosition.x <= pansPos[pansPos.Length - 1].x && !isScrolling) ;
            scrollRect.inertia = false;
    
        float nearesPos = float.MaxValue;
        for (int i = 0; i < panCount; i++)
        {
 
            float distance = Mathf.Abs(contentRect.anchoredPosition.x - pansPos[i].x);
 
            if (distance < nearesPos)
            {
                nearesPos = distance;
                selectedPanID = i;
            }
            float scale = Mathf.Clamp(1 / (distance / panOffset) * scaleOffset, 0.5f, 1f);
            pansScale[i].x = Mathf.SmoothStep(instPans[i].transform.localScale.x, scale + 0.3f, scaleSpeed * Time.fixedDeltaTime);
            pansScale[i].y = Mathf.SmoothStep(instPans[i].transform.localScale.y, scale + 0.3f, scaleSpeed * Time.fixedDeltaTime);
            instPans[i].transform.localScale = pansScale[i];
        }
        float scrollVelocity = Mathf.Abs(scrollRect.velocity.x);
        if (scrollVelocity < 400 && !isScrolling) scrollRect.inertia = false;
 
        if (isScrolling || scrollVelocity > 400) return;  
        contentVector.x = Mathf.SmoothStep(contentRect.anchoredPosition.x, pansPos[selectedPanID].x, snapSpeed * Time.fixedDeltaTime);
        contentRect.anchoredPosition = contentVector;
    }
    public void Scrolling(bool scroll)
    {
        isScrolling = scroll;
        if (scroll) scrollRect.inertia = true;
    }
 
    
}
Я сделал массив из картинок , и подставил из к клонам и работает
C#
1
instPans[i].GetComponent<Image>().sprite = sprites[i];
там просто указал количество и перетянул картини , но как сделать это со сценами , чтобы было по клике на выбранный уровень"картинку, экземпляр " подгружалась сцена (2 , 3 , 4 , 5 ,6 )Помогите!
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
28.01.2021, 14:52
Ответы с готовыми решениями:

Как уничтожить клон префаба?
Помогите пожалуйста с такой проблемой. Создаю взрывы префабом, в котором сидит Particle System....

Проблема с созданием префаба с SpringerJoint2D Unity2D
Здравствуйте! У меня проблема! Я делаю игру типо Angry Birds, но только мячиком надо попасть в...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
0
28.01.2021, 14:52
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru