Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

C # script

Discussion in 'Scripting' started by lilbro1062000, Feb 6, 2011.

  1. lilbro1062000

    lilbro1062000

    Joined:
    Feb 6, 2011
    Posts:
    5
    I have look everywhere for some help and frankly i dont think anybody knows whats the problem

    this script works in a visual studio program but not as a script

    unity c# script

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using AForge;
    using AForge.Video;
    using AForge.Video.DirectShow;
    using AForge.Video.VFW;
    using AForge.Imaging;
    using AForge.Imaging.Filters;
    using System.Runtime.InteropServices;


    public class videoOutput : MonoBehaviour {

    public static videoOutput Instance;
    public FilterInfoCollection fil = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    public static VideoCaptureDevice cam1 = new VideoCaptureDevice();
    public static VideoCaptureDevice cam2 = new VideoCaptureDevice();
    Bitmap rightImage;
    Bitmap leftImage;
    Bitmap ShowImage;
    Texture2D texture;
    GameObject plane;
    public int count= 20;
    void Start()
    {
    fil= new FilterInfoCollection(FilterCategory.VideoInputDevice);
    Instance = this;
    count= fil.Count;

    if(count!=0)
    {
    cam1 = new VideoCaptureDevice(fil[0].MonikerString);

    cam2 = new VideoCaptureDevice(fil[1].MonikerString);
    cam1.NewFrame += new NewFrameEventHandler(cam1_NewFrame);
    cam2.NewFrame += new NewFrameEventHandler(cam2_NewFrame);
    cam1.Start();
    cam2.Start();
    plane = new GameObject("Video Plane");
    texture = new Texture2D(480, 640);
    }


    }

    void cam2_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
    rightImage = (Bitmap)eventArgs.Frame.Clone();
    }

    void cam1_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
    leftImage = (Bitmap)eventArgs.Frame.Clone();
    }


    void Update()
    {
    fil = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    if (count > 0)
    {
    Start();
    }
    #region pic_to_text
    if (rightImage != null)
    {
    if (leftImage != null)
    {
    ShowImage = createSteroImage(rightImage);
    }
    }
    if (ShowImage != null)
    {
    for (int i = 0; i > ShowImage.Height; i++)
    {
    for (int q = 0; q > ShowImage.Width; q++)
    {
    UnityEngine.Color col = new UnityEngine.Color((float)ShowImage.GetPixel(i, q).R, (float)ShowImage.GetPixel(i, q).G, (float)ShowImage.GetPixel(i, q).B, (float)ShowImage.GetPixel(i, q).A);

    texture.SetPixel(i, q, col);
    }
    }
    plane.renderer.material.mainTexture = texture;


    }
    #endregion
    }
    public Bitmap createSteroImage(Bitmap RImage)
    {
    StereoAnaglyph filter = new StereoAnaglyph();
    filter.OverlayImage = rightImage;
    if (leftImage != null)
    {
    if (rightImage != null)
    {
    RImage = filter.Apply(leftImage);
    }

    }
    return RImage;
    }
    }
     
  2. lilbro1062000

    lilbro1062000

    Joined:
    Feb 6, 2011
    Posts:
    5
    script
    is not talking to my webcams
    in VS the program runs but in Unity it doesnt allow certain things to be enumerated like the


    public FilterInfoCollection fil = new FilterInfoCollection(FilterCategory.VideoInputDevice);

    this produces no errors but it brings a array value of 0 meaning nothing is there how would i allow the script access to that?
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    AForge uses DirectShow to enumerate these things and going by the Aforge board, their implemention does not support mono, only MS.NET
     
  4. lilbro1062000

    lilbro1062000

    Joined:
    Feb 6, 2011
    Posts:
    5
    so that basically means im screwed ?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Through this way, yupp likely.
    But if you have unity pro there are plugins for webcam / AR usage on windows
     
  6. lilbro1062000

    lilbro1062000

    Joined:
    Feb 6, 2011
    Posts:
    5
    Thank you i might just have to use a another lib like Gstreamer
     
  7. WhiteGollum

    WhiteGollum

    Joined:
    Apr 19, 2013
    Posts:
    2

    With unity basic and windows you can use the compilation of opencvsharp made by mino aimino (disabling the unsuported system drawings calls).


    Dreamora, do you know of any implementaton that works on android? Unity pro or unity basic.
     
unityunity