首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一元游戏-绘制线路生成电路

问一元游戏-绘制线路生成电路
EN

Stack Overflow用户
提问于 2018-12-17 15:00:54
回答 1查看 1.1K关注 0票数 1

我不是一个团结的初学者,但到目前为止还不是;)今天,我想做一个混合池弹球游戏。比赛对我来说很好,但我最大的挑战是:我想做什么

让玩家画他自己的电路,然后放置不同的项目:保险杠,粘性墙.

我用3D制作它,但是用相机的位置,它会像2D一样。首先,我不知道如何做弯曲的游戏对象。我知道bezier曲线生成器,但不知道如何用它生成形状。

理想情况下,如果我可以:画一条线(用鼠标或查找器的位置)像一个线渲染组件,然后统一挤出一个方向的场电路。我有我的电路。第二个挑战是放置不同的部件(鼠标或手指):保险杠,玩家.有一些规则:一个粘性的墙壁必须在一个简单的墙上从赛道,而不是在中间,你必须始终保留一些空间,为球等。然后我们创建一个游戏电路,然后我们可以玩:):)

我用的是最后一个统一版本。

显然,我并不期待一个完整的解决方案:rolleyes:,但根据你的说法,这有可能吗?哪条路,我该学哪种技术?哪个可能是大比索?任何评论意见都是好的。

我已经开始看https://unity3d.com/fr/learn/tutori...ation-tutorial/creating-meshes?playlist=17153了,我不知道它是不是太复杂了?

非常感谢你的帮助,阿克塞尔

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 07:25:08

在这里我的解决方案

代码语言:javascript
复制
using System.IO;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Networking;
public class scri : MonoBehaviour
{
// For saving the mesh------------------------ 
public KeyCode saveKey = KeyCode.F12;
public string saveName = "SavedMesh";

// Concerning mesher--------------------------
public GameObject mesher; //require

public List<Vector3> vertices;
public  List<int> triangles;

public Vector3 point0;
public Vector3 point1;
public Vector3 point2;
public Vector3 point3;

public int loop;
public float size;

public Mesh meshFilterMesh;
public Mesh meshColliderMesh;

// Sprite work
public Color[] pixels;

public Texture2D newTexture;  
public Texture2D oldTexture;  //require

private Sprite mySprite;
private SpriteRenderer spriteRenderer;

public int pathCount;

public GameObject displayerComponent; //require

public PolygonCollider2D polygonColliderAdded; //require

void Start()
{
    // Mesher
    vertices = new List<Vector3> (); 
    triangles = new List<int> (); 
    meshFilterMesh= mesher.GetComponent<MeshFilter>().mesh;
    meshColliderMesh= mesher.GetComponent<MeshCollider>().sharedMesh;
    size = 10; // lenght of the mesh in Z direction
    loop=0;

    // Sprite
    pixels = oldTexture.GetPixels();
    newTexture =new Texture2D(oldTexture.width,oldTexture.height,TextureFormat.ARGB32, false);
    spriteRenderer = gameObject.AddComponent<SpriteRenderer>();

    ConvertSpriteAndCreateCollider (pixels);
    BrowseColliderToCreateMesh (polygonColliderAdded);

}

void Update()
{
    // Save if F12 press
    if (Input.GetKeyDown(saveKey)){SaveAsset();}
}

public void ConvertSpriteAndCreateCollider (Color[] pixels) {
    for (int i = 0 ; i < pixels.Length ; i++ ) 
    { 
        // delete all black pixel (black is the circuit, white is the walls)
        if ((pixels[i].r==0 && pixels[i].g==0 && pixels[i].b==0 && pixels[i].a==1)) {
            pixels[i] = Color.clear;
        }
    }
    // Set a new texture with this pixel list
    newTexture.SetPixels(pixels);
    newTexture.Apply();

    // Create a sprite from this texture
    mySprite = Sprite.Create(newTexture, new Rect(0, 0, newTexture.width, newTexture.height), new Vector2(10.0f,10.0f), 10.0f, 0, SpriteMeshType.Tight,new Vector4(0,0,0,0),false);

    // Add it to our displayerComponent
    displayerComponent.GetComponent<SpriteRenderer>().sprite=mySprite;

    // Add the polygon collider to our displayer Component and get his path count
    polygonColliderAdded = displayerComponent.AddComponent<PolygonCollider2D>();

}

// Method to browse the collider and launch makemesh
public void BrowseColliderToCreateMesh (PolygonCollider2D polygonColliderAdded){
    //browse all path from collider
    pathCount=polygonColliderAdded.pathCount;
    for (int i = 0; i < pathCount; i++)
    {
        Vector2[] path = polygonColliderAdded.GetPath(i);

        // browse all path point
        for (int j = 1; j < path.Length; j++)
        {
            if (j != (path.Length - 1)) // if we aren't at the last point
            {
            point0 = new Vector3(path[j-1].x ,path[j-1].y ,0);
            point1 = new Vector3(path[j-1].x ,path[j-1].y ,size);
            point2 = new Vector3(path[j].x ,path[j].y ,size);
            point3 = new Vector3(path[j].x ,path[j].y ,0);
                MakeMesh(point0,point1,point2,point3);

            } 
            else if(j == (path.Length - 1))// if we are at the last point, we need to close the loop with the first point
            {
            point0 = new Vector3(path[j-1].x ,path[j-1].y ,0);
            point1 = new Vector3(path[j-1].x ,path[j-1].y ,size);
            point2 = new Vector3(path[j].x ,path[j].y ,size);
            point3 = new Vector3(path[j].x ,path[j].y ,0);
                MakeMesh(point0,point1,point2,point3);
            point0 = new Vector3(path[j].x ,path[j].y ,0);
            point1 = new Vector3(path[j].x ,path[j].y ,size);
            point2 = new Vector3(path[0].x ,path[0].y ,size); // First point
            point3 = new Vector3(path[0].x ,path[0].y ,0); // First point
                MakeMesh(point0,point1,point2,point3);
            }
        }
    }
}


//Method to generate 2 triangles mesh from the 4 points 0 1 2 3 and add it to the collider
public void MakeMesh (Vector3 point0,Vector3 point1,Vector3 point2, Vector3 point3){

    // Vertice add
    vertices.Add(point0);
    vertices.Add(point1);
    vertices.Add(point2);
    vertices.Add(point3);

    //Triangle order
    triangles.Add(0+loop*4);
    triangles.Add(2+loop*4);
    triangles.Add(1+loop*4);
    triangles.Add(0+loop*4);
    triangles.Add(3+loop*4);
    triangles.Add(2+loop*4);
    loop = loop + 1; 

    // create mesh 
    meshFilterMesh.vertices=vertices.ToArray();
    meshFilterMesh.triangles=triangles.ToArray();

    // add this mesh to the MeshCollider
    mesher.GetComponent<MeshCollider>().sharedMesh=meshFilterMesh;
}

// Save if F12 press
public void SaveAsset() 
{
    var mf = mesher.GetComponent<MeshFilter>();
    if (mf)
    {
        var savePath = "Assets/" + saveName + ".asset";
        Debug.Log("Saved Mesh to:" + savePath);
        AssetDatabase.CreateAsset(mf.mesh, savePath);
    }
}

}

​

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53817810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档