在android中用unity创建形状

时间:2016-03-22 12:35:59

标签: c# android unity5

我正试图用自由手创造出统一的自定义形状,但我得到的形状很厚很像这样 enter image description here

我正在使用此代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class DrawLine : MonoBehaviour
{
    public Text toshow;
    private LineRenderer line;
    private bool isMousePressed;
    public List<Vector3> pointsList;
    private Vector3 mousePos;

    // Structure for line points
    struct myLine
    {
        public Vector3 StartPoint;
        public Vector3 EndPoint;
    };
    //  ----------------------------------- 
    void Awake ()
    {
        // Create line renderer component and set its property
        line = gameObject.AddComponent<LineRenderer> ();
        line.material = new Material (Shader.Find ("Particles/Additive"));
        line.SetVertexCount (0);
        line.SetWidth (0.1f, 0.1f);
        line.SetColors (Color.green, Color.green);
        line.useWorldSpace = true;  
        isMousePressed = false;
        pointsList = new List<Vector3> ();
        //      renderer.material.SetTextureOffset(
    }
    // Following method adds collider to created line
    private void addColliderToLine(Vector3 startPos, Vector3 endPos)
    {
        BoxCollider col = new GameObject("Collider").AddComponent<BoxCollider> ();
        col.gameObject.tag = "Box";
        col.transform.parent = line.transform; // Collider is added as child object of line
        float lineLength = Vector3.Distance (startPos, endPos); // length of line
        col.size = new Vector3 (lineLength, 0.1f, 1f); // size of collider is set where X is length of line, Y is width of line, Z will be set as per requirement
        Vector3 midPoint = (startPos + endPos)/2;
        col.transform.position = midPoint; // setting position of collider object
        // Following lines calculate the angle between startPos and endPos
        float angle = (Mathf.Abs (startPos.y - endPos.y) / Mathf.Abs (startPos.x - endPos.x));
        if((startPos.y<endPos.y && startPos.x>endPos.x) || (endPos.y<startPos.y && endPos.x>startPos.x))
        {
            angle*=-1;
        }
        angle = Mathf.Rad2Deg * Mathf.Atan (angle);
        col.transform.Rotate (0, 0, angle);
    }
    void createCollider(){
        for (int i=0; i < pointsList.Count - 1; i++) {
            addColliderToLine(pointsList[i],pointsList[i+1]);
        }
    }
    public void MouseDrag(){
        #if UNITY_ANDROID
            Vector2 touchPos = Input.touches[0].position;
            mousePos = Camera.main.ScreenToWorldPoint (new Vector3(touchPos.x, touchPos.y, 0));
            mousePos.z = 0;
            if (!pointsList.Contains (mousePos)) {
                pointsList.Add (mousePos);
                line.SetVertexCount (pointsList.Count);
                line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
            }
        #endif
    }
    public void MouseBeginDrag(){
        isMousePressed = true;
        line.SetVertexCount (0);
        GameObject[] ob = GameObject.FindGameObjectsWithTag("Box");
        for(int i=0; i<ob.Length; i++){
            Destroy(ob[i]);
        }
        pointsList.RemoveRange (0, pointsList.Count);
        line.SetColors (Color.green, Color.green);
    }
    public void MouseFinishDrag(){
        createCollider();
        isMousePressed = false;
    }
    //  ----------------------------------- 
    void Update ()
    {
    }
}

请帮我减少一条线的宽度。我无法减小线条的宽度。请帮帮我

0 个答案:

没有答案