I created a pair of nav buttons (next and previous) to navigate through an array of images but something stange is happening at the beginning and end of the array. I have to hit the next or previous button twice to move forward or back. Once I do that at the beginning or end of the array everything works normally. Can someone see what's going wrong?
public Texture2D tex;
public string[] galleryImages;
public Button nextBtn;
public Button prevBtn;
int randomIndex;
int currentIndex = 0;
public Color inactiveColor = new Color(0.2F, 0.3F, 0.4F, 0.5F);
string[] arctopithecusImages;
string[] arctopithecusPNGImages;
string[] gulonImages;
string[] scythianWolfImages;
string[] simivulpaImages;
string[] succorathImages;
string[] tatusImages;
// System.Random rand = new System.Random();
// Create a master Array of all image files located in all Image locations
void Start()
{
// Build Gallery Arrays
arctopithecusImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg");
arctopithecusPNGImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.png");
gulonImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/GULON/", "*.jpg");
scythianWolfImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/SCYTHIAN-WOLF/", "*.png");
simivulpaImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/SIMIVULPA/", "*.png");
succorathImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/SUCCORATH/", "*.png");
tatusImages = Directory.GetFiles(#"/Users/kenmarold/Screenshots/TATUS/", "*.png");
// Concatenate all Folder Array into single Array
galleryImages =
arctopithecusImages.Concat(arctopithecusPNGImages)
.Concat(gulonImages)
.Concat(scythianWolfImages)
.Concat(simivulpaImages)
.Concat(succorathImages)
.Concat(tatusImages)
.ToArray();
Debug.Log(galleryImages.Length);
}
IEnumerator loader(int indexNum)
{
WWW www = new WWW("file://" + galleryImages[indexNum]); // get the first file from disk
yield return www; // Wait unill its loaded
tex = new Texture2D(512,512); // create a new Texture2D
www.LoadImageIntoTexture(tex); // put the image file into the new Texture2D
Rect rct = new Rect(0, 0, tex.width, tex.height);
Vector2 pvt = new Vector2(0.5f, 0.5f);
GameObject screenShotImg = GameObject.FindGameObjectWithTag("GalleryImgHolder");
Image img = screenShotImg.GetComponent<Image>();
img.sprite = Sprite.Create(tex, rct, pvt);
}
public void LoadImg()
{
if(tex == null)
{
StartCoroutine("loader", currentIndex);
Debug.Log(currentIndex);
} else {
currentIndex = Random.Range(0,galleryImages.Length);
StartCoroutine("loader", currentIndex);
Debug.Log(currentIndex);
}
}
public void nextImage()
{
Debug.Log(currentIndex);
// Increment through gallery
if(currentIndex != galleryImages.Length - 1)
{
StartCoroutine("loader", currentIndex++);
prevBtn.enabled = true;
prevBtn.interactable = true;
nextBtn.enabled = true;
nextBtn.interactable = true;
// nextBtn.image.color = new Color(255f,255f,255f,1f);
} else if (currentIndex == galleryImages.Length - 1){
nextBtn.enabled = false;
nextBtn.interactable = false;
// nextBtn.image.color = new Color(255f,255f,255f,.5f);
}
}
public void prevImage()
{
Debug.Log(currentIndex);
// Decrement through gallery
if(currentIndex != 0)
{
StartCoroutine("loader", currentIndex--);
nextBtn.enabled = true;
nextBtn.interactable = true;
prevBtn.enabled = true;
prevBtn.interactable = true;
// prevBtn.image.color = new Color(255f,255f,255f,1f);
} else if (currentIndex == 0){
prevBtn.enabled = false;
prevBtn.interactable = false;
// prevBtn.image.color = new Color(255f,255f,255f,.5f);
}
}
}
Your yield return is the problem, that's not how its supposed to be used, if you want to wait for asynchronous methods to finish, learn how to use async/await.
Related
I have a list of images(Helper.theaterImagesInfo) that i am reading through a foreach loop and continuing infinite loop using while until a form closed. images display well with the code below
Application.DoEvents();
await Task.Delay(img.durationInMilliseconds);//img.durationInMilliseconds
this.PictureBox1.Image = img.ImageHere;
Now i want to implement fade effect to those images displaying. Each image is displayed after 5 seconds.
Function to apply fade:
private int intCount = 0;
private List<Bitmap> lstFade = new List<Bitmap>();
private void ApplyFade(Bitmap imgTarget, Bitmap imgSource)
{
int intX = 0;
int intY = 0;
int intAmount = 10;
Rectangle rctDest = new Rectangle(intX, intY,
imgTarget.Width, imgTarget.Height);
using (var tmpBitMap = new Bitmap(imgSource))
using (var gTemp = Graphics.FromImage(tmpBitMap))
for (int i = 0; i <= intAmount; i++)
{
gTemp.DrawImage(imgSource, intX, intY);
ColorMatrix cmColors = new ColorMatrix();
cmColors.Matrix33 = System.Convert.ToSingle(i /
(double)intAmount);
ImageAttributes iaAttributes = new ImageAttributes();
iaAttributes.SetColorMatrix(cmColors);
gTemp.DrawImage(imgTarget, rctDest, intX, intY,
imgTarget.Width, imgTarget.Height, GraphicsUnit.Pixel,
iaAttributes);
lstFade.Add((Bitmap)tmpBitMap.Clone());
}
}
Timer:
private void timer1_Tick(object sender, EventArgs e)
{
intCount += 2;
if (intCount > 10)
{
timer1.Stop();
}
else
{
PictureBox1.Image = lstFade[intCount];
}
}
and here reading list and applying fade to images
public int x=1;
private async void TheaterForm_Shown(object sender, EventArgs e)
{
if (this.PictureBox1.Image == null) SetDefaultImage();
if (Helper.theaterImagesInfo != null && Helper.theaterImagesInfo.Count >
0)
{
while (x == 1)
{
foreach (var img in Helper.theaterImagesInfo.ToList())
{
if (img.IsAnimated)
{
try
{
Application.DoEvents();
var imgS = new Bitmap(this.PictureBox1.Image);
await Task.Delay(img.durationInMilliseconds);
var imgT = new Bitmap(img.ImageHere);
ApplyFade(imgT, imgS);
intCount = 0;
timer1.Interval = 200;
timer1.Start();
}
catch { }
}
else
{
try
{
Application.DoEvents();
await Task.Delay(img.durationInMilliseconds);
var imgS = new Bitmap(this.PictureBox1.Image);
var imgT = new Bitmap(img.ImageHere);
ApplyFade(imgT, imgS);
intCount = 0;
timer1.Interval = 200;
timer1.Start();
}
catch { }
}
if (this.x == 0)
{
break; // exit the for loop early
}
}
}
SetDefaultImage();
}
}
Problem
Fade is applied between starting two images and then keep on the same fading process with those two images. Seems like ApplyFade function doesn't apply again the imgSource and imgTarget once passed through the parameter
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PrefabReplace : EditorWindow
{
[SerializeField] private GameObject prefab;
private bool selectionChanged;
private string objectsToSearch = "";
private List<GameObject> foundObjects = new List<GameObject>();
private List<GameObject> duplicatedObjects = new List<GameObject>();
private bool searched = false;
private int count = 0;
private int countChilds = 0;
private bool countChildren = false;
[MenuItem("Tools/Prefab Replace")]
static void CreateReplaceWithPrefab()
{
int width = 340;
int height = 300;
int x = (Screen.currentResolution.width - width) / 2;
int y = (Screen.currentResolution.height - height) / 2;
GetWindow<PrefabReplace>().position = new Rect(x, y, width, height);
}
private void OnGUI()
{
Searching();
GUILayout.Space(50);
Replacing();
}
private void Searching()
{
GUI.Label(new Rect(10, 20, 150, 20), "Search by name");
objectsToSearch = GUI.TextField(new Rect(90, 60, 150, 20), objectsToSearch, 25);
if (objectsToSearch != "")
{
GUI.enabled = true;
}
else
{
GUI.enabled = false;
}
GUILayout.Space(40);
if (GUILayout.Button("Search"))
{
foundObjects = new List<GameObject>();
duplicatedObjects = new List<GameObject>();
countChildren = true;
countChilds = 0;
count = 0;
foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
{
if (gameObj.name == objectsToSearch)
{
count += 1;
foundObjects.Add(gameObj);
}
}
if (foundObjects.Count > 0)
{
searched = true;
}
else
{
searched = false;
}
}
GUI.enabled = true;
if (count > 0)
GUI.TextField(new Rect(90, 85, 60, 15), count.ToString(), 25);
if (foundObjects.Count > 0 && countChildren == true)
{
for (int i = 0; i < foundObjects.Count; i++)
{
if (foundObjects[i].transform.childCount > 0)
{
countChilds += foundObjects[i].transform.childCount;
//GameObject duplicate = Instantiate(foundObjects[i]);
//duplicate.name = foundObjects[i].name;
//duplicatedObjects.Add(duplicate);
}
}
countChildren = false;
}
GUI.enabled = true;
if (countChilds > 0)
GUI.TextField(new Rect(90, 105, 60, 15), countChilds.ToString(), 25);
GUILayout.Space(100);
if (foundObjects.Count > 0)
EditorGUILayout.LabelField("Test");
}
private void Replacing()
{
GUILayout.Space(20);
GUILayout.BeginVertical(GUI.skin.box);
GUILayout.Label("Replacing");
GUILayout.Space(20);
prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);
var selection = Selection.objects.OfType<GameObject>().ToList();
if (selectionChanged)
{
if (selection.Count == 0)
GUI.enabled = false;
for (var i = selection.Count - 1; i >= 0; --i)
{
var selectedObject = selection[i];
if (prefab != null && selection.Count > 0 &&
selectedObject.scene.name != null
&& prefab != PrefabUtility
.GetCorrespondingObjectFromSource(selectedObject))
{
GUI.enabled = true;
}
else
{
GUI.enabled = false;
}
}
}
else
{
GUI.enabled = false;
}
if (GUILayout.Button("Replace"))
{
InstantiatePrefab(selection);
selectionChanged = false;
}
GUILayout.Space(10);
GUI.enabled = true;
EditorGUILayout.LabelField("Selection count: " + Selection.objects.OfType<GameObject>().Count());
GUILayout.EndVertical();
}
private void OnInspectorUpdate()
{
Repaint();
}
private void OnSelectionChange()
{
selectionChanged = true;
}
private void InstantiatePrefab(List<GameObject> selection)
{
if (prefab != null && selection.Count > 0)
{
for (var i = selection.Count - 1; i >= 0; --i)
{
var selected = selection[i];
SceneManager.SetActiveScene(SceneManager.GetSceneByName(selected.scene.name));
var prefabType = PrefabUtility.GetPrefabType(prefab);
GameObject newObject;
if (prefabType == PrefabType.Prefab)
{
newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
}
else
{
newObject = Instantiate(prefab);
newObject.name = prefab.name;
}
if (newObject == null)
{
Debug.LogError("Error instantiating prefab");
break;
}
Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
newObject.transform.parent = selected.transform.parent;
newObject.transform.localPosition = selected.transform.localPosition;
newObject.transform.localRotation = selected.transform.localRotation;
newObject.transform.localScale = selected.transform.localScale;
newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
Undo.DestroyObjectImmediate(selected);
}
}
}
}
The searching part find object/s in the hierarchy and if there are any childs it's counting them too.
The replacing part replace a prefab with selection of objects. If for example I selected 20 Cubes and clicked the Replace button it will replace the selected Cubes with the prefab with the same position,rotation,scaling the cubes was.
But it's not replacing childs if there are any.
I need somehow when searching to add to a List/s the parent and childs and keep the hierarchy of each parent and his childs and then when clicking the replace button to replace it will the searching results objects and not with the selection like now and including the childs.
Example if my Hierarchy looks like this:
Cube1
Cube1
Cube1
Cube1
Cube1
Cube1
If I'm searching for Cube1 I shold find 6 Cubes and two of them childs.
And if for example I will replace them with Sphere prefab the Hierarchy should looks like:
Sphere
Sphere
Sphere
Sphere
Sphere
Sphere
What I need to do is to combine the searching part with the replacing part and make that the replacing will include also childs.
You could try something like this:
void ChangeObjectRecursively(Transform t)
{
for (int i = 0; i < t.childCount; i++)
{
ChangeObjectRecursively(t.getChild(i));
}
ChangeObject(t.gameObject);
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.SceneManagement;
public static class TransformSaver
{
[System.Serializable]
public class TransformInfo
{
public string sceneName;
public string name;
public Transform parent;
public Vector3 pos;
public Quaternion rot;
public Vector3 scale;
}
//Save Transform
public static void SaveTransform(Transform[] tranformToSave)
{
TransformInfo[] trnfrm = new TransformInfo[tranformToSave.Length];
for (int i = 0; i < trnfrm.Length; i++)
{
trnfrm[i] = new TransformInfo();
trnfrm[i].sceneName = tranformToSave[i].gameObject.scene.name;
trnfrm[i].name = tranformToSave[i].name;
trnfrm[i].parent = tranformToSave[i].parent;
trnfrm[i].pos = tranformToSave[i].localPosition;
trnfrm[i].rot = tranformToSave[i].localRotation;
trnfrm[i].scale = tranformToSave[i].localScale;
}
string jsonTransform = JsonHelper.ToJson(trnfrm, true);
File.WriteAllText(#"d:\json\json.txt", jsonTransform);
}
//Load Transform
public static Transform[] LoadTransform()
{
string jsonTransform = File.ReadAllText(#"d:\json\json.txt");
if (jsonTransform == null)
{
return null;
}
TransformInfo[] savedTransforms = JsonHelper.FromJson<TransformInfo>(jsonTransform);
GameObject[] gameObjects = new GameObject[savedTransforms.Length];
Transform[] loadedTransforms = new Transform[savedTransforms.Length];
for (int i = 0; i < gameObjects.Length; i++)
{
SceneManager.SetActiveScene(SceneManager.GetSceneByName(savedTransforms[i].sceneName));
gameObjects[i] = new GameObject();
loadedTransforms[i] = gameObjects[i].transform;
loadedTransforms[i].name = savedTransforms[i].name;
loadedTransforms[i].parent = savedTransforms[i].parent;
loadedTransforms[i].localPosition = savedTransforms[i].pos;
loadedTransforms[i].localRotation = savedTransforms[i].rot;
loadedTransforms[i].localScale = savedTransforms[i].scale;
}
return loadedTransforms;
}
}
The problem is that the method LoadTransform will return array of Transforms but also will create new GameObjects in the Editor in the Hierarchy.
What I want to do is to add something like a boolean flag or maybe other way so I can select if toe return the array of transforms with or without creating new gameobjects so I tried something like that:
//Load Transform
public static Transform[] LoadTransform(bool returnInfo)
{
string jsonTransform = File.ReadAllText(#"d:\json\json.txt");
if (jsonTransform == null)
{
return null;
}
TransformInfo[] savedTransforms = JsonHelper.FromJson<TransformInfo>(jsonTransform);
GameObject[] gameObjects = new GameObject[savedTransforms.Length];
Transform[] loadedTransforms = new Transform[savedTransforms.Length];
for (int i = 0; i < gameObjects.Length; i++)
{
SceneManager.SetActiveScene(SceneManager.GetSceneByName(savedTransforms[i].sceneName));
if (returnInfo == true)
{
gameObjects[i] = new GameObject();
loadedTransforms[i] = gameObjects[i].transform;
}
loadedTransforms[i].name = savedTransforms[i].name;
loadedTransforms[i].parent = savedTransforms[i].parent;
loadedTransforms[i].localPosition = savedTransforms[i].pos;
loadedTransforms[i].localRotation = savedTransforms[i].rot;
loadedTransforms[i].localScale = savedTransforms[i].scale;
}
return loadedTransforms;
}
I added a bool flag returnInfo but now loadedTransforms is null.
But the main goal is to decide if to return array of transforms with or without making instances for new gameobjects.
The problem is when I'm using Editorwindow type script and inside OnGUI to save and load the objects:
List<GameObject> selections = Selection.objects.OfType<GameObject>().ToList();
if (selections.Count > 0)
{
GUI.enabled = true;
}
else
{
GUI.enabled = false;
}
if (GUILayout.Button("Save selected objects data"))
{
if (selections.Count > 0)
{
tempTransformSelection = selections;
for (var i = selections.Count - 1; i >= 0; --i)
{
var selected = selections[i];
transformSelection.Add(selected.transform);
}
TransformSaver.SaveTransform(transformSelection.ToArray());
transformSelection = new List<Transform>();
}
}
var file = #"d:\json\json.txt";
FileInfo fi = new FileInfo(file);
// By default asume you don't want to show the button
// it will than only be enabled if the later conditions match
bool showButton = false;
// you can check this already here not in the loop
// if no file -> nothing to do
if (!File.Exists(#"d:\json\json.txt") || fi.Length <= 0) return;
// This is only reached if the file exists and is not empty
// check for null transforms
for (int i = 0; i < tempTransformSelection.Count(); i++)
{
// if not null do nothing
if (tempTransformSelection[i] != null)
continue;
// otherwise enable the button and leave the loop
showButton = true;
break;
}
// if not true then button won't be shown
if (showButton == true)
{
GUI.enabled = true;
}
else
{
GUI.enabled = false;
}
if (GUILayout.Button("Instantiate back deleted selected bojects"))
{
TransformSaver.LoadTransform(true);
showButton = false;
}
}
private void OnInspectorUpdate()
{
Repaint();
}
The main goal is to select objects in the hierachy in the editor. Then to click on "Save selected objects data" to save the selected object to a json file.
Then I want to make that only if one of the saved objects to the file is null in the hierarchy fro example if I deleted one or more of the objects in the saved file ! only then enable true the button "Instantiate back deleted selected bojects"
And then when clicking on the button "Instantiate back deleted selected bojects" set the button back to enabled false !
But the important thing is to check for null against the saved list of objects in the json file ! But once again I'm stuck with the tempTransformSelection and with how to enable false/true the button.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PrefabReplace : EditorWindow
{
[SerializeField] private GameObject prefab;
GameObject newobj;
[MenuItem("Tools/Prefab Replace")]
static void CreateReplaceWithPrefab()
{
EditorWindow.GetWindow<PrefabReplace>();
}
private void OnGUI()
{
prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);
if (GUILayout.Button("Replace"))
{
var selection = Selection.gameObjects.ToList();
if (prefab != null && selection.Count > 0)
{
for (var i = selection.Count - 1; i >= 0; --i)
{
var selected = selection[i];
var prefabType = PrefabUtility.GetPrefabType(prefab);
GameObject newObject;
if (prefabType == PrefabType.Prefab)
{
newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
}
else
{
newObject = Instantiate(prefab);
newObject.name = prefab.name;
}
if (newObject == null)
{
Debug.LogError("Error instantiating prefab");
break;
}
Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
newObject.transform.parent = selected.transform.parent;
newObject.transform.localPosition = selected.transform.localPosition;
newObject.transform.localRotation = selected.transform.localRotation;
newObject.transform.localScale = selected.transform.localScale;
newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
Undo.DestroyObjectImmediate(selected);
}
}
}
if (GUILayout.Button("Write settings"))
{
var selection = Selection.gameObjects.ToList();
if (selection.Count > 0)
{
for (int i = selection.Count - 1; i >= 0; --i)
{
var selected = selection[i].transform;
string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
WriteDataToFile(toWrite);
}
}
}
if (GUILayout.Button("Read settings"))
{
var obj = GetObjectFromFile("Assets/Resources/test.txt");
Instantiate(obj);
DestroyImmediate(newobj);
}
GUI.enabled = false;
EditorGUILayout.LabelField("Selection count: " + Selection.objects.Length);
}
private void OnInspectorUpdate()
{
Repaint();
}
private void WriteDataToFile(string line)
{
string path = "Assets/Resources/test.txt";
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine(line);
writer.Close();
}
private GameObject GetObjectFromFile(string path)
{
newobj = new GameObject();
string[] text = new string[3];
if (File.Exists(path))
{
text = File.ReadAllLines(path);
foreach (string s in text)
{
text = s.Split(':');
break;
}
var parent = text[0].Substring(0, text[0].IndexOf(" ("));
var localPosition = StringToVector3(text[1]);
var localRotation = StringToQuaternion(text[2]);
var localScale = StringToVector3(text[3]);
if (parent != null)
{
GameObject toparent = GameObject.Find(parent);
newobj.transform.parent = toparent.transform;
}
newobj.transform.localPosition = localPosition;
newobj.transform.localRotation = localRotation;
newobj.transform.localScale = localScale;
}
return newobj;
}
public static Vector3 StringToVector3(string sVector)
{
// Remove the parentheses
if (sVector.StartsWith("(") && sVector.EndsWith(")"))
{
sVector = sVector.Substring(1, sVector.Length - 2);
}
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]));
return result;
}
public static Quaternion StringToQuaternion(string sQuaternion)
{
// Remove the parentheses
if (sQuaternion.StartsWith("(") && sQuaternion.EndsWith(")"))
{
sQuaternion = sQuaternion.Substring(1, sQuaternion.Length - 2);
}
// split the items
string[] sArray = sQuaternion.Split(',');
// store as a Vector3
Quaternion result = new Quaternion(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]),
float.Parse(sArray[3]));
return result;
}
}
When I put a break point on the line:
DestroyImmediate(newobj);
I see that the variable obj is fine and not null.
But then when I click continue once I see that the variable obj is null.
I'm destroying the newobj then why it can't remember the values that was in obj ? I don't want to make new instance for obj since it will add a new GameObject to the hierarchy. I want only to assign the values to a new gameobject.
If you want to assign a new object with the same data use the structure of:
var newobj = new Type(oldObj);
This will copy the contents across and leave you with 2 seperate objects.
E.g var obj = new ObjectClass(GetObjectFromFile("Assets/Resources/test.txt"));
I started to experiment with the new Unity beta 5.6 Video Player. I have made a media player to loop images and videos from a folder (url) on a RawImage.
When I add larger video files (trailers) to the folder, at some point pretty soon the Player gets stuck after the video has ended and does not leave the while (videoPlayer.isPlaying) loop. At same time the rendering stops completely from the profiler tool. This does not happen with smaller & shorter video clips.
Also there is a small bug with audio. The audio wont play on first loop with first video file. After that it plays fine.
Have I made some mistake or could this be a bug in the beta? I have only tested with .mp4 videos.
Edit: Seems like a workaround fix for now is to add videoPlayer.isLooping = true; after videoPlayer.Play(); command. Code updated.
Code so far ->
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using UnityEngine.Video;
public class Play : MonoBehaviour
{
// Use this for initialization
bool develop = true;
bool web = false;
//UI & Audio
private VideoPlayer videoPlayer;
private VideoSource videoSource;
private AudioSource audioSource;
//Local
private string path = "file://";
private string folder = "C:/medias/";
private WaitForSeconds waitTime;
int arrLength;
int i = 0;
//Web (http)
private string webpath = "http://";
string uri = "www.rtcmagazine.com/files/images/5353/";
string source;
string str;
WWW www;
//Extensions to get
string[] extensions = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".mp4", ".MP4", ".webm", ".WEBM", ".avi", ".AVI" };
FileInfo[] info;
DirectoryInfo dir;
void Start()
{
//Add VideoPlayer to the GameObject
videoPlayer = gameObject.AddComponent<VideoPlayer>();
//Add AudioSource
audioSource = gameObject.AddComponent<AudioSource>();
//Disable Play on Awake for both Video and Audio
videoPlayer.playOnAwake = true;
audioSource.playOnAwake = true;
if (web) {
string[] fetchFilesCount = GetFiles (webpath+uri);
int getLenght = fetchFilesCount.Length;
info = new FileInfo[getLenght];
int counter = 0;
string[] filesFromWeb = GetFiles (webpath+uri);
foreach (String file in filesFromWeb)
{
info [counter] = new FileInfo(webpath+uri+file);
counter++;
if (counter == filesFromWeb.Length) {
counter = 0;
}
}
} else {
info = new FileInfo[]{};
dir = new DirectoryInfo(#folder);
info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
}
arrLength = info.Length;
Application.runInBackground = true;
StartCoroutine(looper());
}
IEnumerator looper()
{
//Run forever
while (true)
{
if (i == arrLength - 1)
{
if (web) {
int counter = 0;
string[] filesFromWeb = GetFiles (webpath+uri);
foreach (String file in filesFromWeb)
{
info [counter] = new FileInfo(webpath+uri+file);
counter++;
if (counter == filesFromWeb.Length) {
counter = 0;
}
}
} else {
info = dir.GetFiles ().Where (f => extensions.Contains (f.Extension.ToLower ())).ToArray ();
arrLength = info.Length;
}
i = 0;
}
else
{
i++;
}
//Debug.Log(info[i]);
yield return StartCoroutine(medialogic());
int dotIndex = info[i].ToString().LastIndexOf('.');
//string lhs = index < 0 ? source : source.Substring(0,index),
string searchDot = dotIndex < 0 ? "" : source.Substring(dotIndex+1);
int dotPos = Array.IndexOf(extensions, "." + searchDot);
if (dotPos > 5) {
waitTime = new WaitForSeconds (0);
} else {
waitTime = new WaitForSeconds (7);
}
//Wait for 7 seconds
yield return waitTime;
}
}
IEnumerator medialogic()
{
source = info[i].ToString();
if (web) {
if (develop) {
www = new WWW (source);
} else {
www = new WWW (webpath+uri+source);
}
} else {
if (develop) {
str = path + source;
} else {
str = path + folder + source;
}
www = new WWW (str);
}
int index = source.LastIndexOf('.');
//string lhs = index < 0 ? source : source.Substring(0,index),
string rhs = index < 0 ? "" : source.Substring(index+1);
int pos = Array.IndexOf(extensions, "." + rhs);
if (pos > -1 && pos < 6)
{
//Wait for download to finish
yield return www;
GetComponent<RawImage>().texture = www.texture;
}
else if (pos > 5)
{
//videos here
videoPlayer.source = VideoSource.Url;
videoPlayer.url = str;
//Set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
//Set video To Play then prepare Audio to prevent Buffering
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
//Assign the Texture from Video to RawImage to be displayed
GetComponent<RawImage>().texture = videoPlayer.texture;
//Play Video
videoPlayer.Play();
//Play Sound
audioSource.Play();
//Set loop to true, hack for a freeze bug
videoPlayer.isLooping = true;
//Alternative way to check if video has ended
videoPlayer.loopPointReached += EndReached;
Debug.Log ("Play started");
while (videoPlayer.isPlaying)
{
Debug.Log ("---Playing---");
yield return null;
}
Debug.Log("Done Playing Video");
}
}
void EndReached(UnityEngine.Video.VideoPlayer videoPlayer) {
Debug.Log("End reached!");
//Play Video
videoPlayer.Stop();
//Play Sound
audioSource.Stop();
}
public static string[] GetFiles(string url)
{
string[] extensions2 = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".mp4", ".MP4", ".webm", ".WEBM", ".avi", ".AVI" };
List<string> files = new List<string>(500);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regex = new Regex("(?<name>.*)");
MatchCollection matches = regex.Matches(html);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
if (match.Success)
{
string[] matchData = match.Groups[0].ToString().Split('\"');
foreach (string x in extensions2)
{
if (match.ToString().Contains(x))
{
files.Add(matchData[1]);
}
}
//files.Add(matchData[1]);
}
}
}
}
}
return files.ToArray();
}
public static string[] getFtpFolderItems(string ftpURL)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
request.Method = WebRequestMethods.Ftp.ListDirectory;
//You could add Credentials, if needed
//request.Credentials = new NetworkCredential("anonymous", "password");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
}
Thanks for your time :)