Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tatsunomi · Oct 05, 2013 at 03:43 PM · errorcs0029converting-var-type-to-other-var-type

Video Workaround in Unity

I've been trying to place all my images inside one gameObject and change its texture material depending on the frame rate & Time.time. I couldn't seem to find a workaround for converting my texture materials(objects) as an int so i could increment it as if it were an actual video with the right amount of FPS. I was also wondering if I could load a set of images inside one folder which can be located in multiple directories? I put comments in the code too.

Here is the code:

 using UnityEngine;
 using System.Collections;
 //Dynamic Arrays require using collections.generics
 using System.Collections.Generic; //Make a generic list?
 
 
 public class VideoT : MonoBehaviour {
     public List<int> totalpictures; //Public list
     string imageFolderName = "Biglang Liko - Ron Henley feat (10-4-2013 6-42-24 PM)";
     public bool MakeTexture= false;
     //ArrayList pictures= new ArrayList();
     //public int[0] totalpictures = 6000; 
     bool loop= false;
     int counter= 0;
     bool Film = true;
     float PictureRateInSeconds = 1;
     private float nextPic = 0;
     Object[] texturesL; // all the pictures for the video treated as a list of objects
 
  
     void  Start (){
         totalpictures = new List<int>()[6000];
         if(Film == true){
             PictureRateInSeconds = 0.04166666666666666666f;
         }
  
         //Object[] textures = Resources.LoadAll(imageFolderName);
             texturesL = Resources.LoadAll(imageFolderName); //Is it possible to load resources from multiple directories? a->b->c->picturesfolder
             int[] textures = texturesL as int; //
         for(int i= 0; i < textures.Length; i++){
             Debug.Log("found TEXTURES");
             totalpictures.Add(textures[i]);
         }
     }
  
     void  Update (){
         if(Time.time > nextPic){
             nextPic = Time.time + PictureRateInSeconds; // FPS
             counter += 1;
             if(MakeTexture == true){
                 renderer.material.mainTexture = totalpictures[counter]; //
             }
         }
         if(counter >= totalpictures.length){
         //if(counter >= totalpictures.GetRange){
             Debug.Log("exceeding material length");
             if(loop == true){
                 counter = 0; // goes back to first frame
             }
         }
     }
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 05, 2013 at 03:46 PM 0
Share

$$anonymous$$ay be a long shot, but have you tried firing a single Plane out of the particle system then using a Sprite Animation texture sheet to simulate video?

avatar image Tatsunomi · Oct 05, 2013 at 03:52 PM 0
Share

Haven't tried that or even thought of it. So what you're trying to say is that if I tried to fire planes with the textures on them with animation sheets? I don't even know how to do that or know where to start.

avatar image meat5000 ♦ · Oct 05, 2013 at 04:04 PM 0
Share

Shuriken Particle system has Sprite sheet animation, which basically chucks out each tile of a texture each time period. I'm not sure what the texture size limit is though, or what res you can achieve. Basically you need to set Rate to 0 and set Burst Particles to 1 for an individual paticle. $$anonymous$$ake it have 0 velocity and an initial size of your choice.

avatar image Tatsunomi · Oct 05, 2013 at 04:38 PM 0
Share

Wow. Your code is so simple yet it works like a charm but i don't code in javascript but in C#. Well anyway thanks!

avatar image meat5000 ♦ · Oct 05, 2013 at 04:39 PM 0
Share

Convert to answer Eric :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Oct 05, 2013 at 04:24 PM

http://wiki.unity3d.com/index.php?title=Texture_swap_animator

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tatsunomi · Oct 06, 2013 at 06:39 PM 0
Share

I like how the javascript works but I modified it to C# codes but it isn't as fluid as yours.

     public Texture2D[] frames;
     int theTime;
     public int framesPerSecond = 10;
  
     void  Update (){
         theTime = $$anonymous$$athf.RoundToInt(Time.time);
         int index = theTime * framesPerSecond;
         index = index % frames.Length;
         renderer.material.mainTexture = frames[index];
         }

I needed a C# version of it but this is as close as it gets but it isn't as smooth than the javascript version. Is there like a way to implicitly or explicitly convert int to float? (for the renderer.material line)

avatar image TrickyHandz · Oct 06, 2013 at 07:15 PM 0
Share

You can cast an int to a float like this:

 int myInt = 1;
 float myFloat = (float)myInt;
avatar image Hoeloe · Oct 06, 2013 at 07:38 PM 0
Share

Also, if you have a constant value, you can declare it as a float by postfixing the letter F. This is useful because as long as one value in a calculation is a float, the entire calculation will be calculated with floats. For example:

 int i = 5;
 float f = 5 * i/2f;

Will give you 12.5, while:

 int i = 5;
 float f = 5 * i/2;

Will give you 10. The way to think about it is that there is no basic operation that operates on ints and floats at the same time. The operation 1/2 uses the simpler type int, as it wasn't specified otherwise, so 1 is an int, 2 is an int, and it operates using integer division, giving the value 0 (since it can only return an int). However, the operation 1/2f does not operate on an int and a float, ins$$anonymous$$d it implicitly casts the int to a float, because it has to cast in one direction, and floats cannot be implicitly cast to ints. Therefore, it converts the 1 into the float 1.0f, and runs the operation 1.0f/2.0f, which is floating point division, returning the expected value of 0.5f. Note that with floating point division, 4f/2 will still be floating point division, and will return the value 2f, with type float, not type int.

avatar image Eric5h5 · Oct 06, 2013 at 08:29 PM 1
Share

A straight Unityscript to C# conversion is:

 public Texture2D[] frames;
 public float framesPerSecond = 10.0f;
  
 void Update () {
     int index = (int)(Time.time * framesPerSecond);
     index = index % frames.Length;
     renderer.material.mainTexture = frames[index];
 }

Don't make global variables unless you absolutely need to.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Compile Errors for Script 1 Answer

Error CS0029 fix 1 Answer

Raycast Ray ray origin problem 3 Answers

Error cs0029: Cannot implicitly convert type `void' to `bool' 1 Answer

[Closed] Getting CS0029 error when I try to check position of an object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges