MovieTexture Never Plays In Build - unity3d

I have written a script to play OGG files as a MovieTexture, i've tried both embedded asset files and also from the web. The problem I'm having is that the videos play in debug move (When I hit the play button and test in the "Game" tab) but they never work when I build to an executable..
using UnityEngine;
namespace Assets.Scripts
{
[RequireComponent(typeof(AudioSource))]
public class VideoScreen : MonoBehaviour
{
public string videoUrl;
public bool autoPlay = true;
public bool loop = true;
public bool playAudio = true;
public float opacity = 1.0f;
private bool hasLoaded;
private MovieTexture movieTexture;
public void Start ()
{
if (string.IsNullOrEmpty(videoUrl))
{
return;
}
var data = new WWW(videoUrl);
movieTexture = data.movie as MovieTexture;
}
public void Update()
{
if (movieTexture.isReadyToPlay && !hasLoaded)
{
renderer.material = new Material(Shader.Find("Custom/Unlit Transparent Color")) { mainTexture = movieTexture };
audio.clip = movieTexture.audioClip;
SetLoop(loop);
if (autoPlay)
{
Play();
}
hasLoaded = true;
}
var textureColor = renderer.material.color;
textureColor.a = opacity;
renderer.material.color = textureColor;
}
private void Play()
{
movieTexture.Stop();
movieTexture.Play();
if (playAudio)
{
audio.Stop();
audio.Play();
}
}
private void Pause()
{
movieTexture.Pause();
if (playAudio)
{
audio.Pause();
}
}
private void Stop()
{
movieTexture.Stop();
if (playAudio)
{
audio.Stop();
}
}
private void SetLoop(bool loopStatus)
{
movieTexture.loop = loopStatus;
if (playAudio)
{
audio.loop = loopStatus;
}
}
}
}
Can anyone shed any light on this behaviour?
Thanks

Turns out the custom Transparent Diffuse shader I was using was causing the issue! Doh!!
renderer.material = new Material(Shader.Find("Custom/Unlit Transparent Color")) { mainTexture = movieTexture };
now becomes
renderer.material.mainTexture = movieTexture;

Related

Score text is not increasing in Unity After Seconds

I am following one of the Udemy tutorials where it shows how to increase score after each second.
I have exactly done whatever he showed and I checked the code 10 times and the codes are the same but the score is not increasing.
Here is my GameManager.cs code:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public GameObject PlatformSpawner;
public bool gameStarted;
public bool gameEnd;
// Start is called before the first frame update
public Text scoreText;
int score = 0;
void Awake()
{
if (instance == null)
{
instance = this;
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
if(!gameStarted)
{
if (Input.GetMouseButtonDown(0))
{
GameStart();
}
}
}
public void GameStart()
{
gameStarted = true;
PlatformSpawner.SetActive(true);
}
public void GameOver()
{
gameStarted = false;
gameEnd = true;
PlatformSpawner.SetActive(false);
}
IEnumerator UpdateScore()
{
while (true)
{
yield return new WaitForSeconds(1f);
score += 1;
scoreText.text = score.ToString();
}
}
}
And here is the photo of unity where I have added scoreText in GameManager

In Unity, line of code keeps blocking video in WebGL?

I want to play a video in WebGL but it keeps blocking my video due to one line of code and I don't know why or how to "repair" it. When clicking on play, only one frame is shown and then it stops. (Entire code below).
The issue is this particular line in my Update function:
if (video.isPlaying)
{
if (!video.isPrepared) return;
videoSlider.value = (float)NTime; //This line here!!!
}
If not commented out, the video wont play, for whatever reason.
NTime, which seems to cause the issue, is:
video.time/ ((ulong)(video.frameCount / video.frameRate)) //video is videoPlayer
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using TMPro;
public class SliderVideo : MonoBehaviour
{
[SerializeField] VideoPlayer video;
[SerializeField] Slider videoSlider;
[SerializeField] Slider volumeSlider;
[SerializeField] Button playButton;
[SerializeField] RawImage fullScreenImage;
[SerializeField] RectTransform buttons;
[SerializeField] Vector3 buttonsPos;
Vector3 buttonsPosCache;
[SerializeField] TextMeshProUGUI buttonLanguage;
[SerializeField] GameObject disableButton;
[SerializeField] GameObject videoRectangle;
int cacheWidth, cacheHeight;
float videoWidth, videoHeight;
float videoWidthCache, videoHeightCache;
double videoTimeCache;
bool fullScreen;
float timerMouse;
public bool languageChange = false;
public string videoFileName;
public string videoFileNameEN;
public string videoFileNameDE;
//Start
private void Start()
{
playButton.image.sprite = Resources.LoadAll<Sprite>("icons")[25];
videoRectangle.SetActive(false);
videoHeight = fullScreenImage.gameObject.GetComponent<RectTransform>().sizeDelta.y;
videoWidth = fullScreenImage.gameObject.GetComponent<RectTransform>().sizeDelta.x;
buttonsPosCache = buttons.anchoredPosition;
}
//Update
private void Update()
{
if (video.isPlaying)
{
if (!video.isPrepared) return;
videoSlider.value = (float)NTime;
video.SetDirectAudioVolume(0, volumeSlider.value);
}
if (fullScreen)
{
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
timerMouse = 0;
buttons.gameObject.SetActive(true);
}
else
{
timerMouse += Time.deltaTime;
if (timerMouse > 3)
{
timerMouse = 3;
buttons.gameObject.SetActive(false);
}
}
}
}
public ulong Duration
{
get { return (ulong)(video.frameCount / video.frameRate); }
}
public double VideoTime
{
get { return video.time; }
}
public double NTime
{
get { return VideoTime / Duration; }
}
//Prepare Method
public void LoadVideo(string source)
{
video.url = System.IO.Path.Combine(Application.streamingAssetsPath, source);
video.Prepare();
}
//Methods for Buttons
public void PlayVideo()
{
//if (!IsPrepared && IsPlaying) return ;
video.Play();
}
public void PauseVideo()
{
if (!video.isPlaying) return;
video.Pause();
}
public void SeekSlider()
{
video.time = (videoSlider.value * Duration);
}
public void PauseResume()
{
//if (!IsPrepared) return ;
videoRectangle.SetActive(true);
if (video.isPlaying)
{
playButton.image.sprite = Resources.LoadAll<Sprite>("icons")[25];
PauseVideo();
}
else if (!video.isPlaying)
{
playButton.image.sprite = Resources.LoadAll<Sprite>("icons")[24];
PlayVideo();
}
}
public void FullScreen()
{
if (!fullScreen)
{
disableButton.SetActive(false);
fullScreenImage.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(1920, 1920);
buttons.anchoredPosition = buttonsPos;
fullScreen = true;
}
else
{
disableButton.SetActive(true);
fullScreenImage.gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(videoWidth, videoHeight);
buttons.anchoredPosition = buttonsPosCache;
fullScreen = false;
}
}
public void ChangeVideoLanguage()
{
if (!languageChange)
{
buttonLanguage.text = "EN";
videoFileName = videoFileNameDE;
video.Stop();
playButton.image.sprite = Resources.LoadAll<Sprite>("icons")[25];
LoadVideo(videoFileName);
languageChange = true;
}
else
{
buttonLanguage.text = "DE";
videoFileName = videoFileNameEN;
video.Stop();
playButton.image.sprite = Resources.LoadAll<Sprite>("icons")[25];
LoadVideo(videoFileName);
languageChange = false;
}
}
}

I can't jump and move at the same time unity2d

i made 2d character and 3 ui buttons and they worked well
but the problem is when moving to the right or left by the ui buttons i can't jump however when jump from the ui button i can move to right and left
this is the script
public class PlayerWalk : MonoBehaviour {
private PlayerAnimation playerAnim;
private Rigidbody2D myBody;
private SpriteRenderer spriteRenderer;
public float speed = 7f;
public float jumpForce = 7f;
private bool moveLeft; // determine if we move left or right
private bool dontMove; // determine if we are moving or not
private bool canJump; // we will test if we can jump
void Start () {
playerAnim = GetComponent<PlayerAnimation>();
myBody = GetComponent<Rigidbody2D>();
dontMove = true;
}
void Update () {
//DetectInput();
HandleMoving();
}
void HandleMoving() {
if (dontMove) {
StopMoving();
} else {
if (moveLeft) {
MoveLeft();
} else if (!moveLeft) {
MoveRight();
}
}
} // handle moving
public void AllowMovement(bool movement) {
dontMove = false;
moveLeft = movement;
}
public void DontAllowMovement() {
dontMove = true;
}
public void Jump() {
if(canJump) {
myBody.velocity = new Vector2(myBody.velocity.x, jumpForce);
//myBody.AddForce(Vector2.right * jumpForce);
}
}
// PREVIOUS FUNCTIONS
public void MoveLeft() {
myBody.velocity = new Vector2(-speed, myBody.velocity.y);
playerAnim.ZombieWalk(true, true);
}
public void MoveRight() {
myBody.velocity = new Vector2(speed, myBody.velocity.y);
playerAnim.ZombieWalk(true, false);
}
public void StopMoving() {
playerAnim.ZombieStop();
myBody.velocity = new Vector2(0f, myBody.velocity.y);
}
void DetectInput() {
float x = Input.GetAxisRaw("Horizontal");
if (x > 0)
{
MoveRight();
}
else if (x < 0)
{
MoveLeft();
}
else
{
StopMoving();
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(collision.gameObject.tag == "Ground") {
canJump = true;
}
}
void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.tag == "Ground") {
canJump = false;
}
}
} // class
the 2d character moves well and there is no bugs or problems with scripts
Any help??
I don't know where is the problem??
**Note ** i used unity5.6
I would say onTriggerEnter2D instead of using onCollisionEnter2D would be a better option in this scenario. You can read more about that here.
https://answers.unity.com/questions/875770/ontriggerenter-or-oncollisionenter-1.html#:~:text=OnCollisionEnter%20is%20called%20when%20two,with%20%22IsTrigger%22%20set).
Did you try to debug the value of canJump while you are trying to move left or right?

Unity Ads Stuck in Loop

I have a test ad setup in my unity game. I am calling the IntAd function from the GameManager Script after the game is over. But the problem is when I click the close button in the ad, the ad reappears again and again. Please help me how can stop IntAd function after showing the ad.
public class AdsManager : MonoBehaviour
{
public static AdsManager instance;
private string playStoreID = "000000";
private string intAd = "video";
private string rewardedAd = "rewardedVideo";
private string bannerAd = "bannerAd";
public bool isTargrtPlayStore;
public bool isTestAd;
public static int start;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
if (instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
}
}
private void Start()
{
InitializeAdvertisement();
}
private void InitializeAdvertisement()
{
if (isTargrtPlayStore)
{
Advertisement.Initialize(playStoreID, isTestAd);
return;
}
}
public void IntAd()
{
if (Advertisement.IsReady(intAd))
{
Advertisement.Show(intAd);
}
}
}
Here is GameManager Script-
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public bool gameOver;
void Awake()
{
if (instance == null){
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
gameOver = false;
}
// Update is called once per frame
void Update()
{
}
public void StartGame()
{
UiManager.instance.GameStart();
UiManager.instance.StartScore1();
ScoreManager.instance.startScore();
GameObject.Find("PlatformSpawner").GetComponent<PlatformSpawner>().StartSpawningPlatforms();
//AdsManager.instance.BannerAd();
}
public void GameOver()
{
UiManager.instance.GameOver();
ScoreManager.instance.stopScore();
gameOver = true;
AdsManager.instance.IntAd();
}
}
code to call GameOver function
void Update()
{
if (!started)
{
if (Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector3(speed, 0, 0);
started = true;
GameManager.instance.StartGame();
}
}
if (!Physics.Raycast(transform.position, Vector3.down, 1f))
{
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
GameManager.instance.GameOver();
}
The code you have calling GameManager.GameOver() is inside of Update() so it's going to get called every frame, and if !Physics.Raycast(transform.position, Vector3.down, 1f) is always true when the game is over (and not just true once), then it'll end up calling GameManager.GameOver() every frame which lines up with the behaviour that you're seeing.
You could resolve this by adding !gameOver to your if statement, that way it'll only go into the if statement once at the end of your game, and then upon gamestart you could flip gameOver back to false.
if (!Physics.Raycast(transform.position, Vector3.down, 1f) && !gameOver)
{
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
GameManager.instance.GameOver();
}
Just do an if statement with a bool named GameEnd. Set it to false. Play the ad only if GameEnd is false. Rite after the ad instantiates, set GameEnd to true.
if (!Physics.Raycast(transform.position, Vector3.down, 1f))
{
bool GameEnd = false;
gameOver = true;
rb.velocity = new Vector3(0, -25f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
if(! GameEnd)
{
GameManager.instance.GameOver();
GameEnd = true;
}
}

Long Press Button issue on moving mouse

I use a long press button, which works very well, on MouseDown and if I don't move the mouse, at the end of my delay, I have my action.
If I move the mouse (still pressing down and still on the button) it resets my button's delay, and I don't understand why.
if anyone has an idea, it would help me a lot.
Thx
Here's my code :
private bool _PointerDown;
public float DelaiReponse;
private float _PointerDownTimer;
private bool _IsValidate = false;
private float _Delai;
public void OnPointerDown(PointerEventData eventData)
{
_PointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
_PointerDown = false;
_PointerDownTimer = 0;
}
void Start()
{
_Delai = DelaiReponse;
}
private void Update()
{
if (_PointerDown) // Timer Button Validation Hold
{
_PointerDownTimer += Time.deltaTime;
if (_PointerDownTimer >= _Delai)
{
_IsValidate = true;
}
}
}enter code here
Using Input.GetMouseDown(0) and Input.GetMouseButtonUp(0) could fix the problem:
private void Update()
{
if (Input.GetMouseDown(0))
{
_PointerDown = true;
}
if (Input.GetMouseUp(0))
{
_PointerDown = false;
_PointerDownTimer = 0;
}
if (_PointerDown) // Timer Button Validation Hold
{
:
:
}
:
:
}
I finally found
OnMouseDown even worked outside my button, and I preferred the OnPointerDown method.
I added IDragHandler to my class, and create a bool _PointerDrag
private bool _PointerDown;
private bool _PointerDrag;
public float DelaiReponse;
private float _PointerDownTimer;
private bool _IsValidate = false;
private float _Delai;
public class LongClickButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public void OnPointerDown(PointerEventData eventData)
{
_PointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
_PointerDown = false;
_PointerDownTimer = 0;
}
public void OnDrag(PointerEventData eventData)
{
_PointerDrag = true;
}
void Start()
{
_Delai = DelaiReponse;
}
private void Update()
{
if (_PointerDown || _PointerDrag) // Timer Button Validation Hold
{
_PointerDownTimer += Time.deltaTime;
if (_PointerDownTimer >= _Delai)
{
_IsValidate = true;
}
}
}
}
and it works.
Thanks for your time, Frenchy