Unity   发布时间:2019-10-13  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Unity LineRenderer制作画版大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

source:

m.Collections; m.Collections.Generic; ENGIne;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> AtwalPaint : MonoBehaviour {
<span style="color: #0000ff">public<span style="color: #000000"> Material currentMaterial;

</span><span style="color: #0000ff"&gt;private</span> Color paintColor =<span style="color: #000000"&gt; Color.red;
</span><span style="color: #0000ff"&gt;private</span> <span style="color: #0000ff"&gt;float</span> paintSize = <span style="color: #800080"&gt;0.1f</span><span style="color: #000000"&gt;;
</span><span style="color: #0000ff"&gt;private</span> <span style="color: #0000ff"&gt;bool</span> isPress = <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;;
</span><span style="color: #0000ff"&gt;private</span><span style="color: #000000"&gt; LineRenderer lineRenderer;
</span><span style="color: #0000ff"&gt;private</span> List<Vector3> positions = <span style="color: #0000ff"&gt;new</span> List<Vector3><span style="color: #000000"&gt;();
</span><span style="color: #0000ff"&gt;private</span> <span style="color: #0000ff"&gt;int</span> lineCount = <span style="color: #800080"&gt;0</span><span style="color: #000000"&gt;;

</span><span style="color: #0000ff"&gt;private</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; update()
{
    </span><span style="color: #0000ff"&gt;if</span> (Input.GetMouseButtonDown(<span style="color: #800080"&gt;0</span><span style="color: #000000"&gt;))
    {
        isPress </span>= <span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;;
        GameObject go </span>= <span style="color: #0000ff"&gt;new</span> GameObject(<span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;LineRenderer_</span><span style="color: #800000"&gt;"</span> +<span style="color: #000000"&gt; lineCount);
        go.transform.parent </span>=<span style="color: #000000"&gt; gameObject.transform;
        lineRenderer </span>= go.AddComponent<LineRenderer><span style="color: #000000"&gt;();
        lineRenderer.startColor </span>=<span style="color: #000000"&gt; paintColor;
        lineRenderer.endColor </span>=<span style="color: #000000"&gt; paintColor;
        lineRenderer.startWidth </span>=<span style="color: #000000"&gt; paintSize;
        lineRenderer.endWidth </span>=<span style="color: #000000"&gt; paintSize;
        lineRenderer.material </span>=<span style="color: #000000"&gt; currentMaterial;
        lineRenderer.numCapVertices </span>= <span style="color: #800080"&gt;5</span><span style="color: #000000"&gt;;
        lineRenderer.numCornerVertices </span>= <span style="color: #800080"&gt;5</span><span style="color: #000000"&gt;;
        lineCount</span>++<span style="color: #000000"&gt;;
        AddPosition();
    }

    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isPress)
    {
        AddPosition();
    }

    </span><span style="color: #0000ff"&gt;if</span> (Input.GetMouseButtonUp(<span style="color: #800080"&gt;0</span><span style="color: #000000"&gt;))
    {
        lineRenderer </span>= <span style="color: #0000ff"&gt;null</span><span style="color: #000000"&gt;;
        positions.Clear();
        isPress </span>= <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;;
    }
}

</span><span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; AddPosition()
{
    Vector3 position </span>=<span style="color: #000000"&gt; GetMousePoint();
    </span><span style="color: #0000ff"&gt;if</span> (positions.Count > <span style="color: #800080"&gt;0</span><span style="color: #000000"&gt;)
    {
        </span><span style="color: #0000ff"&gt;if</span> (Vector3.Distance(position,positions[positions.Count - <span style="color: #800080"&gt;1</span>]) < <span style="color: #800080"&gt;0.1f</span><span style="color: #000000"&gt;)
        {
            </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt;;
        }
    }
    position.z </span>= -<span style="color: #800080"&gt;0.02f</span> *<span style="color: #000000"&gt; lineCount;
    positions.Add(position);
    lineRenderer.positionCount </span>=<span style="color: #000000"&gt; positions.Count;
    lineRenderer.SetPositions(positions.ToArray());
}

Vector3 GetMousePoint()
{
    Ray ray </span>=<span style="color: #000000"&gt; Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    </span><span style="color: #0000ff"&gt;bool</span> isCollider = Physics.Raycast(ray,<span style="color: #0000ff"&gt;out</span><span style="color: #000000"&gt; hit);
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isCollider)
    {
        </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; hit.point;
    }
    </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; Vector3.zero;
}


</span><span style="color: #0000ff"&gt;#region</span> color
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> OnRedColorChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintColor </span>=<span style="color: #000000"&gt; Color.red;
    }
}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> OnGreenColorChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintColor </span>=<span style="color: #000000"&gt; Color.green;
    }
}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> OnBlueColorChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintColor </span>=<span style="color: #000000"&gt; Color.blue;
    }
}
</span><span style="color: #0000ff"&gt;#endregion</span>

<span style="color: #0000ff"&gt;#region</span> size
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> On1SizeChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintSize </span>= <span style="color: #800080"&gt;0.1f</span><span style="color: #000000"&gt;;
    }
}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> On2SizeChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintSize </span>= <span style="color: #800080"&gt;0.2f</span><span style="color: #000000"&gt;;
    }
}
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;void</span> On4SizeChange(<span style="color: #0000ff"&gt;bool</span><span style="color: #000000"&gt; isOn)
{
    </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (isOn)
    {
        paintSize </span>= <span style="color: #800080"&gt;0.4f</span><span style="color: #000000"&gt;;
    }
}
</span><span style="color: #0000ff"&gt;#endregion</span><span style="color: #000000"&gt;

}

大佬总结

以上是大佬教程为你收集整理的Unity LineRenderer制作画版全部内容,希望文章能够帮你解决Unity LineRenderer制作画版所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: