Button Capture get captured in android - unity3d

i make an augmented reality project using capture image.
in my project i use button capture for capture the picture
but when i capture my button get captured too
this is the code
using UnityEngine;
using System.Collections;
using System.IO;
public class buttonCapture : MonoBehaviour {
public Texture iconCapture;
//private int filename;
void OnGUI(){
if (GUI.Button (new Rect (145, 530, 100, 45), iconCapture))
{
string filename = "image.jpg";
Application.CaptureScreenshot(filename);
if (Vuforia.QCARRuntimeUtilities.IsPlayMode()) {
// if in PlayMode, the screenshot will be saved
// to the project directory
Debug.Log ("/storage/ " + filename);
}
else {
// if running on Device, the screenshot will be saved
// to the Application.persistentDataPath directory
Debug.Log ("/storage/" + Application.persistentDataPath + "/" + filename);
}
}
}
}

Create a bool you set to true while capturing the screenshot and set it false when done.
Use this bool to show and hide the button.

Related

Unity, scaling is changing, but i do not wrote any code or change anything

I am making a small 2D game with a Youtube tutorial. Now I am trying to add floattext. When I show my text, the scale of my text goes from 1 to 540, thus text is as big as player cannot see it. An example image is below:
as shown, the text is so big. Its scale is somehow 540.
I make UI's (Canvas's) render mode to "Screen Space - Camera", then drag the main camera to canvas. After I've selected the "UI scale mode" to "Scale With Size". My reference resolution is 1920*1080. The reference pixel per unit is 1.
If I drop text prefab to the scene, the text is showing with normal size. But if I call it with collapsing (When my character touches a chest, then text occurs) size of the text is huge.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FloatingTextManager : MonoBehaviour
{
public GameObject textContainer;
public GameObject textPrefab;
private List<FloatingText> floatingTexts = new List<FloatingText>();
private void Update() {
foreach (FloatingText txt in floatingTexts)
{
txt.UpdateFloationgText();
}
}
public void Show(string msg, int fontSize, Color color, Vector3 position, Vector3 motion, float duration) {
FloatingText floatingText = GetFloatingText();
floatingText.txt.text = msg;
floatingText.txt.fontSize = fontSize;
floatingText.txt.color = color;
floatingText.go.transform.position = position;
floatingText.motion = motion;
floatingText.duration = duration;
floatingText.Show();
}
private FloatingText GetFloatingText() {
FloatingText txt = floatingTexts.Find(t => !t.active);
if (txt == null){
txt = new FloatingText();
txt.go = Instantiate(textPrefab);
txt.go.transform.SetParent(textContainer.transform);
txt.txt = txt.go.GetComponent<Text>();
floatingTexts.Add(txt);
}
return txt;
}
}
those are (above) the "floating text manager script"
My hierarchy and project. "FloatingText" game object occur at under the "FLoatingTextManager
The codes at the below are chest's code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chest : Collectable // inherited everything from "collidable.
//including mono behaviour.
{
public Sprite emptyChest;
public int pesosAmount;
protected override void OnCollect() {
if (!collected) {
collected = true;
pesosAmount = Random.Range(5, 10);
GetComponent<SpriteRenderer>().sprite = emptyChest;
GameManager.instance.ShowText("+" + pesosAmount + " pesos!", 45, Color.yellow, gameObject.transform.position, Vector3.up * 0, 10);
}
}
}
I've solved it by adding a new "scale" variable to my floating text.

Get active scene when next scene loads

So I've been trying to make a game but when I press the button the scene reloads itself. What I want to do is make it so that whenever a scene is loaded it gets the build index of the active scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelSwitch : MonoBehaviour
{
int CurrentSceneBuildIndex;
bool sceneLoaded;
void Update()
{
if (sceneLoaded == true )
{
CurrentSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
}
}
public void NextLevel()
{
SceneManager.LoadScene(CurrentSceneBuildIndex + 3 );
sceneLoaded = true;
}
public void LevelSelect()
{
SceneManager.LoadScene("LevelSelect");
}
}
The issue:
Ok, so the issue with your solution is that when a new scene is loaded in Unity, GameObjects and their attached scripts are not kept between scenes. In your scripts function NextLevel() you are loading the scene and then setting the sceneLoaded boolean to true however this is after the new scene is loaded and we know that GameObjects and their attached scripts do not transfer between scenes which means this boolean value does not transfer to the next scene either. This is unless a GameObject is marked with dontdestroyonload.
So what can we do to fix this?
To get the current build index of the scene when it is loaded you can attach a script to a GameObject in the scene you want to get the index of which uses SceneManager.sceneLoaded to detect when the scene is finished loading and then get the index.
For example:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExampleCode : MonoBehaviour
{
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded; // This tells the script to call the function "OnSceneLoaded" when the scene manager detects the scene has finished loading with the parameters of the scene object and the mode of how the scene was loaded
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name + ", Build Index : " + scene.buildIndex.ToString());
Debug.Log("Load Mode : " + mode);
}
// called third
void Start()
{
Debug.Log("Start");
}
// called when the game or scene closes
void OnDisable()
{
Debug.Log("OnDisable");
SceneManager.sceneLoaded -= OnSceneLoaded; // This tells the script to stop calling OnSceneLoaded when the scene manager detects a new scene has been loaded
}
}

Unity, Vr, Vive - Controllers To Play / Pause a Video?

I am new and currently trying to get my Vive Controllers to Pause / Play Unity. So far i Can see my "hands" and it does Recognise my triggers, which is all it needs to.
Does Anyone know how to make it Pause when I press the Trigger and then Start when I press it again ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class Viveinput : MonoBehaviour
{
[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
public bool paused;
void Update () {
if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
{
print(" Grab Pinch Up");
}
float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);
if (triggerValue > 00f)
{
print(triggerValue);
}
}
}
This is What I am using atm for connection between controller and Unity.
I assume that your video is playing on a VideoPlayer MonoBehaviour :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class Viveinput : MonoBehaviour
{
public VideoPlayer video;
[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
private bool _triggered = false;
void Update () {
if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
{
print(" Grab Pinch Up");
}
float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);
if (triggerValue > 0f && !_triggered)
{
_triggered = true; // This will prevent the following code to be executed each frames when pressing the trigger.
if(!video.isPlaying) { // You dont need a paused boolean as the videoplayer has a property for that.
video.Play();
} else {
video.Pause();
}
} else {
_triggered = false;
}
}
}
You need to drag and drop the VideoPlayer in the editor and it should be it.

How can I simply toggle between Cameras if I press a certain key?

Okay so I have 2 cameras set up in my hierarchy in Unity:
I'd like to know, When in-game, how can I toggle between both cameras when a certain key is pressed? I'm aware I'd maybe need to make a script for this, just not sure how I'd go about doing it.
You can add multiple cameras
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
// Use this for initialization
public Camera[] cameras;
private int currentCameraIndex;
// Use this for initialization
void Start () {
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i=1; i<cameras.Length; i++)
{
cameras[i].gameObject.SetActive(false);
}
//If any cameras were added to the controller, enable the first one
if (cameras.Length>0)
{
cameras [0].gameObject.SetActive (true);
Debug.Log ("Camera with name: " + cameras [0].GetComponent<Camera>().name + ", is now enabled");
}
}
// Update is called once per frame
void Update () {
//If the c button is pressed, switch to the next camera
//Set the camera at the current index to inactive, and set the next one in the array to active
//When we reach the end of the camera array, move back to the beginning or the array.
}
public void Change()
{
currentCameraIndex ++;
Debug.Log ("C button has been pressed. Switching to the next camera");
if (currentCameraIndex < cameras.Length)
{
cameras[currentCameraIndex-1].gameObject.SetActive(false);
cameras[currentCameraIndex].gameObject.SetActive(true);
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].GetComponent<Camera>().name + ", is now enabled");
}
else
{
cameras[currentCameraIndex-1].gameObject.SetActive(false);
currentCameraIndex = 0;
cameras[currentCameraIndex].gameObject.SetActive(true);
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].GetComponent<Camera>().name + ", is now enabled");
}
}
}
Extremely basic question, you should go to some c# tutorials.
At any rate, this will do. Put this in the Update method:
if (Input.GetKeyDown("space"))
{
//don't forget to set one as active either in the Start() method
//or deactivate 1 camera in the Editor before playing
if (Camera1.active == true)
{
Camera1.SetActive(false);
Camera2.SetActive(true);
}
else
{
Camera1.SetActive(true);
Camera2.SetActive(false);
}
}

Unable to load movie via WWW

I'm trying to load a video via url, but I keep getting the same error. I'm using Unity 5.3 and the example code from http://docs.unity3d.com/ScriptReference/WWW-movie.html (heavily modified because the current example doesn't compile).
using UnityEngine;
using System.Collections;
// Make sure we have gui texture and audio source
[RequireComponent (typeof(GUITexture))]
[RequireComponent (typeof(AudioSource))]
public class TestMovie : MonoBehaviour {
string url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
WWW www;
void Start () {
// Start download
www = new WWW(url);
StartCoroutine(PlayMovie());
}
IEnumerator PlayMovie(){
MovieTexture movieTexture = www.movie;
// Make sure the movie is ready to start before we start playing
while (!movieTexture.isReadyToPlay){
yield return 0;
}
GUITexture gt = gameObject.GetComponent<GUITexture>();
// Initialize gui texture to be 1:1 resolution centered on screen
gt.texture = movieTexture;
transform.localScale = Vector3.zero;
transform.position = new Vector3 (0.5f,0.5f,0f);
// gt.pixelInset.xMin = -movieTexture.width / 2;
// gt.pixelInset.xMax = movieTexture.width / 2;
// gt.pixelInset.yMin = -movieTexture.height / 2;
// gt.pixelInset.yMax = movieTexture.height / 2;
// Assign clip to audio source
// Sync playback with audio
AudioSource aud = gameObject.GetComponent<AudioSource>();
aud.clip = movieTexture.audioClip;
// Play both movie & sound
movieTexture.Play();
aud.Play();
}
}
I added this as a script to the Main Camera in a new scene, and I get this error:
Error: Cannot create FMOD::Sound instance for resource (null), (An invalid parameter was passed to this function. )
UnityEngine.WWW:get_movie()
<PlayMovie>c__Iterator4:MoveNext() (at Assets/TestMovie.cs:20)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
TestMovie:Start() (at Assets/TestMovie.cs:16)
(Line 20 is MovieTexture movieTexture = www.movie;)
I've been working on this for a while now, it's happened on many files and both of my systems.
I found a solution! I tested the code with unity 5.2.X and 5.3.X.
I dont know why but Unity 3d requires first wait unitl the download is done with isDone == true and after the copy of the texture wait until the isReadyToPlay == true.
The movie file must be OGG Video format some MP4 files doesn't work.
Well. Check the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MovieTextureStream : MonoBehaviour {
public Text progressGUI;
public MeshRenderer targetRender = null;
public AudioSource targetAudio = null;
public string URLString = "http://unity3d.com/files/docs/sample.ogg";
MovieTexture loadedTexture;
IEnumerator Start() {
if(targetRender ==null) targetRender = GetComponent<MeshRenderer> ();
if(targetAudio ==null) targetAudio = GetComponent<AudioSource> ();
WWW www = new WWW (URLString);
while (www.isDone == false) {
if(progressGUI !=null) progressGUI.text = "Progresso do video: " + (int)(100.0f * www.progress) + "%";
yield return 0;
}
loadedTexture = www.movie;
while (loadedTexture.isReadyToPlay == false) {
yield return 0;
}
targetRender.material.mainTexture = loadedTexture;
targetAudio.clip = loadedTexture.audioClip;
targetAudio.Play ();
loadedTexture.Play ();
}
}
I won't provide a solution with the WWW.movie, but an alternative solution that may help ou or anyone else.
On IPhone, we didn't find a solution to stream video from a server, we decided to download the video before reading it, here's how:
string docPath = Application.persistentDataPath + "/" + id + ".mp4";
if (!System.IO.File.Exists(docPath))
{
WWW www = new WWW(videouUrl);
while(!www.isDone)
{
yield return new WaitForSeconds(1);
Loading.Instance.Message = "Downloading video : " + (int)(www.progress * 100) + "%";
if (!string.IsNullOrEmpty(www.error))
Debug.Log(www.error);
}
byte[] data = www.bytes;
System.IO.File.WriteAllBytes(docPath, data);
}
mediaPlayer.Load(docPath);
onVideoReady();
mediaPlayer.Play();
This is the coroutine used to download and write the video on the iphone's file system. Once it's done, you can load and play it.
Hope it helps.