Wednesday, 15 April 2020

Unity C# Creating dynamic lines using Line render

using System.Collections.Generic;
using UnityEngine;

//[RequireComponent(typeof(LineRenderer))]
public class LineRendererTest : MonoBehaviour
{
    List<Vector3> linePoints = new List<Vector3>();
    LineRenderer lineRenderer;
    public float startWidth = 1.0f;
    public float endWidth = 1.0f;
    public float threshold = 0.001f;
     public Camera thisCamera;
    int lineCount = 0;

    public GameObject linePrefab;
    public bool buttonPressed;
    public List<Vector3> mousePosArray;

    Vector3 lastPos = Vector3.one * float.MaxValue;


    void Awake()
    {
        thisCamera = Camera.main;
    }
    private void Start()
    {

    }
    void Update()
    {
       
        if (Input.GetMouseButton(0))
        {
            if(!buttonPressed)
            {
                linePoints.Clear();
                lineCount = 0;
                buttonPressed = true;
                CreateLine();
            }
        }
        if (Input.GetMouseButton(0))
        {
            UpdateLine();

        }
        if (Input.GetMouseButtonUp(0))
        {
         
            linePoints.Clear();
            lineCount = 0;
            buttonPressed = false;
        }
     
    }

    void CreateLine()
    {
       
        GameObject newLine = Instantiate(linePrefab, Input.mousePosition, Quaternion.identity);
        lineRenderer = newLine.GetComponent<LineRenderer>();

        Vector3 mousePos = Input.mousePosition;
        mousePos.z = thisCamera.nearClipPlane;
        Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);

        float dist = Vector3.Distance(lastPos, mouseWorld);
        if (dist <= threshold)
            return;

        lastPos = mouseWorld;
    }

    void UpdateLine()
    {
       
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = thisCamera.nearClipPlane;
        Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);

        float dist = Vector3.Distance(lastPos, mouseWorld);
        if (dist <= threshold)
            return;

        lastPos = mouseWorld;
        if (linePoints == null)
            linePoints = new List<Vector3>();
        linePoints.Add(mouseWorld);

        lineRenderer.SetWidth(startWidth, endWidth);
        lineRenderer.SetVertexCount(linePoints.Count);

        for (int i = lineCount; i < linePoints.Count; i++)
        {
            lineRenderer.SetPosition(i, linePoints[i]);
        }
        lineCount++;
    }
}

Moving objects in Unity using C# code snippet

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

public class MoveObject : MonoBehaviour
{

    public float moveSpeed;

    // Use this for initialization
    void Start()
    {
        moveSpeed = 1f;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
    }
}

Accessing Camera feed in Unity using C#

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

public class WebStream : MonoBehaviour
{
    public RawImage rawimage;
    WebCamTexture webCamTexture;

    public Text webCamDisplayText;

    void Start()
    {


        WebCamDevice[] cam_devices = WebCamTexture.devices;
        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < cam_devices.Length; i++)
        {
            print("Webcam available: " + cam_devices[i].name);
        }
        GoWebCam01();
    }


    //CAMERA 01 SELECT
    public void GoWebCam01()
    {
        WebCamDevice[] cam_devices = WebCamTexture.devices;
        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < cam_devices.Length; i++)
        {
            print("Webcam available: " + cam_devices[i].name);
        }

        webCamTexture = new WebCamTexture(cam_devices[0].name, 480, 640, 30);
        rawimage.texture = webCamTexture;
        if (webCamTexture != null)
        {
            webCamTexture.Play();
            Debug.Log("Web Cam Connected : " + webCamTexture.deviceName + "\n");
        }
        webCamDisplayText.text = "Camera Type: " + cam_devices[0].name.ToString();
    }
    //CAMERA 02 SELECT
    public void GoWebCam02()
    {
        WebCamDevice[] cam_devices = WebCamTexture.devices;
        // for debugging purposes, prints available devices to the console
        for (int i = 0; i < cam_devices.Length; i++)
        {
            print("Webcam available: " + cam_devices[i].name);
        }

        webCamTexture = new WebCamTexture(cam_devices[1].name, 480, 640, 30);
        rawimage.texture = webCamTexture;
        if (webCamTexture != null)
        {
            webCamTexture.Play();
            Debug.Log("Web Cam Connected : " + webCamTexture.deviceName + "\n");
        }
        webCamDisplayText.text = "Camera Type: " + cam_devices[1].name.ToString();
    }
}