Кроссворд на Unity, помощь с реализацией

Форум для самых маленьких, а так же тех, кому недосуг читать справку самостоятельно.

Кроссворд на Unity, помощь с реализацией

Сообщение SuicideDoll 12 янв 2018, 04:26

Всем Приветик!
Новичок-кодер, бегиннер C# просит помощи с реализацией простого 2D кроссворда под андроид! (ну или не простого :-B )
Какими методами можно осуществить подобное, есть идейки? Буду очень благодарна
SuicideDoll
UNец
 
Сообщения: 1
Зарегистрирован: 12 янв 2018, 04:18

Re: Кроссворд на Unity, помощь с реализацией

Сообщение NAGIBATOR228pacan 13 янв 2018, 09:06

Кидаешь скрипт на любой объект в 2d, сначала создаешь уровень нажав на галочку, потом убираешь галочку и играешь. Пкм создать квадрат с вопросом, Лкм просто квадрат для буквы.
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class crossword : MonoBehaviour {
    [SerializeField]
    bool Создание;
    [SerializeField]
    int Уровень;
    string[,] MAP;
    GameObject canvas;
    GameObject cursor;
    int count=860;
    GameObject obj;
    // Use this for initialization
    void Start () {      
     //   Cursor.visible = false;
        canvas = new GameObject("Canvas");
        canvas.AddComponent<Canvas>();
        canvas.GetComponent<RectTransform>().localScale = new Vector3(0.1f, 0.1f, 0.01f);
        canvas.GetComponent<RectTransform>().transform.position = new Vector3(canvas.transform.position.x+10/2-0.5f, canvas.transform.position.y + 10 / 2 - 0.5f, canvas.transform.position.z + 10 / 2 - 0.5f);
        cursor = GameObject.CreatePrimitive(PrimitiveType.Plane);
        cursor.name = "Cursor";
        cursor.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y));
        cursor.transform.eulerAngles = new Vector3(-90, 0, 0);
        cursor.transform.localScale = new Vector3(0.098f, 0.1f, 0.098f);
        cursor.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
        cursor.GetComponent<Renderer>().material.color = Color.yellow;
        cursor.GetComponent<MeshCollider>().enabled = false;
        MAP = new string[21, 11];
        for (int i = 21; i > 0; i--)
        {
            for (int j = 11; j >0; j--)
            {
                if (PlayerPrefs.GetString("" + i + j+1000*Уровень).Length == 1)
                {
                    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    plane.transform.position = new Vector3(i - 10, j - 5);
                    plane.transform.eulerAngles = new Vector3(-90, 0, 0);
                    plane.transform.localScale = new Vector3(0.098f, 0.1f, 0.098f);
                    plane.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
                    GameObject text = new GameObject("Text");
                    text.transform.parent = canvas.transform;
                    text.AddComponent<Text>();
                    text.transform.position = new Vector3(i - 10, j - 5, -5);
                    text.GetComponent<RectTransform>().transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    text.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
                    text.GetComponent<Text>().fontSize = 90;
                    text.GetComponent<Text>().color = Color.black;
                    text.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
                    plane.transform.parent = text.transform;

                }
                if (PlayerPrefs.GetString("" + i + j+1000*Уровень).Length > 1)
                {
                    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    plane.name = "Вопрос";
                    plane.transform.position = new Vector3(i - 10, j - 5);
                    plane.transform.eulerAngles = new Vector3(-90, 0, 0);
                    plane.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    plane.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
                    plane.GetComponent<Renderer>().material.color = Color.gray;
                    GameObject text = new GameObject("Text");
                    text.transform.parent = canvas.transform;
                    text.AddComponent<Text>();
                    text.transform.position = new Vector3(i - 10, j - 5, -5);
                    text.GetComponent<RectTransform>().transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    text.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
                    text.GetComponent<Text>().fontSize = 10;
                    text.GetComponent<Text>().color = Color.black;
                    text.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
                    plane.transform.parent = text.transform;
                    text.GetComponent<Text>().text = PlayerPrefs.GetString("" + i + j +1000* Уровень);
                    if(!Создание) cursor.GetComponent<MeshCollider>().enabled = false;
                }
             }          
        }
    }
        RaycastHit hit,hit1;
    bool win = false;
    // Update is called once per frame
    void Update()
    {
        if(win == false&& !Создание) {
            for (int i = 0; i < 21; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    if (i == 20 && j == 10)
                    {
                        win = true;
                        int count = 0 ;
                        for (int k = 0; k < 21; k++)
                        {
                            for (int v = 0; v < 11; v++)
                            {
                                GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                                plane.transform.position = new Vector3(k - 10, v - 5);
                                plane.transform.eulerAngles = new Vector3(-90, 0, 0);
                                plane.transform.localScale = new Vector3(0.098f, 0.1f, 0.098f);
                                plane.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
                                plane.GetComponent<Renderer>().material.color = Color.green;
                            }
                        }
                    }
                    RaycastHit hit2;
                    if (Physics.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), new Vector3(i - 10, j - 5) - Camera.main.ScreenToWorldPoint(Input.mousePosition), out hit2))
                    {
                        if (MAP[i,j]!=PlayerPrefs.GetString(""+i+j+1000*Уровень))
                        {
                            i = 21;
                            j = 11;
                        }
                    }
                }
            }
        }
        cursor.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y), -0.001f);
        if (Physics.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.forward, out hit))
        {
            if (Нажато)
            {
                if (Буква != "" + KeyCode.Backspace && Буква.Length == 1)
                {
                    if (Буква != "None")
                    {
                        if (hit.collider.name == "Вопрос")
                        {
                            if (Создание)
                            {
                                MAP[Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) + 10, Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y) + 5] += Буква;
                                hit.collider.gameObject.GetComponentInParent<Text>().text += Буква;
                            }
                        }
                        else
                        {
                            MAP[Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) + 10, Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y) + 5] = Буква;
                            hit.collider.gameObject.GetComponentInParent<Text>().text = Буква;
                        }
                    }
                }
                if (Input.GetKeyDown(KeyCode.Backspace))
                {
                     hit.collider.gameObject.GetComponentInParent<Text>().text = hit.collider.gameObject.GetComponentInParent<Text>().text.Remove(hit.collider.gameObject.GetComponentInParent<Text>().text.Length - 1);                  
                }
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    MAP[Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) + 10, Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y) + 5] += " ";
                    hit.collider.gameObject.GetComponentInParent<Text>().text += " ";
                }
            }
            if(Создание)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    hit.collider.GetComponentInParent<Text>().text="";
                    MAP[Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) + 10, Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y) + 5] = null;
                    Destroy(hit.collider.gameObject);
                }
            }
        }
        else
        {
            if (Создание)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    plane.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y));
                    plane.transform.eulerAngles = new Vector3(-90, 0, 0);
                    plane.transform.localScale = new Vector3(0.098f, 0.1f, 0.098f);
                    plane.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
                    GameObject text = new GameObject("Text");
                    text.transform.parent = canvas.transform;
                    text.AddComponent<Text>();
                    text.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y), -5);
                    text.GetComponent<RectTransform>().transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    text.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
                    text.GetComponent<Text>().fontSize = 90;
                    text.GetComponent<Text>().color = Color.black;
                    text.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
                    plane.transform.parent = text.transform;
                }
                if (Input.GetKeyDown(KeyCode.Mouse1))
                {
                    GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
                    plane.name = "Вопрос";
                    plane.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y));
                    plane.transform.eulerAngles = new Vector3(-90, 0, 0);
                    plane.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    plane.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
                    plane.GetComponent<Renderer>().material.color = Color.gray;
                    GameObject text = new GameObject("Text");
                    text.transform.parent = canvas.transform;
                    text.AddComponent<Text>();
                    text.transform.position = new Vector3(Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).x), Mathf.RoundToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition).y), -5);
                    text.GetComponent<RectTransform>().transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    text.GetComponent<Text>().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
                    text.GetComponent<Text>().fontSize = 10;
                    text.GetComponent<Text>().color = Color.black;
                    text.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
                    plane.transform.parent = text.transform;
                }
            }
        }
    }
    bool Нажато = false;
    string Буква;
    private void OnGUI()
    {
        Event e = Event.current;
        if (e.isKey) Буква = "" + e.keyCode;
        if (e.isKey) Нажато = true;
        else Нажато = false;
    }
    private void OnApplicationQuit()
    {
        if (Создание)
        {
            for (int i = 0; i < 21; i++)
            {
                for (int j = 0; j < 11; j++)
                    PlayerPrefs.SetString("" + i + j+1000*Уровень, MAP[i, j]);
            }
        }
    }
}

 

Мне лень сейчас разбираться что там написано ибо очень давно писал этот кроссворд, так что там могут быть косяки.
NAGIBATOR228pacan
UNITрон
 
Сообщения: 318
Зарегистрирован: 15 мар 2017, 20:39

Re: Кроссворд на Unity, помощь с реализацией

Сообщение Leonin 14 янв 2018, 04:27

Впервые вижу, чтобы кириллицу использовали для названий переменных... :-\
Аватара пользователя
Leonin
UNец
 
Сообщения: 44
Зарегистрирован: 12 янв 2018, 16:59

Re: Кроссворд на Unity, помощь с реализацией

Сообщение lawsonilka 14 янв 2018, 20:59

Leonin писал(а):Впервые вижу, чтобы кириллицу использовали для названий переменных... :-\

да хоть по китайски пишите
lawsonilka
UNIверсал
 
Сообщения: 390
Зарегистрирован: 21 окт 2014, 14:48


Вернуться в Почемучка

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 29