Unity open external link - c#

By placing the unity file on the page I can not redirect the action to another page. What happens is that the new page is opened inside the frame intended for the unity file.
using UnityEngine;
using System.Collections;
public class Urlazerecovelo : MonoBehaviour {
void OnMouseDown ()
{
Application.OpenURL("https://www.google.pt");
}
}

public class OpenWebPage : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Application.ExternalEval("window.open('www.google.pl', '_blank');");
}
}
I assume from what you're saying you use WebGL as target platform. More about here

Related

How to use "OnClick.AddListener" on multiple GameObjects in one Frame

I'm pretty new to Unity and C# scripting.
In my scene I have four buttons. I declared them in my script as public and put the items (on type Button) via drag and drop in my inspector in unity. Depending on which button is clicked, i want my program to do something. You can see in the script below what I've done. That scripts works perfectly just like I want it to.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Button Left;
public Button Right;
public Button Starten;
public Button Zurueck;
private void Start()
{
loadRezept(0);
Right.onClick.AddListener(iterationUp);
Left.onClick.AddListener(iterationDown);
Starten.onClick.AddListener(starten);
Zurueck.onClick.AddListener(zurueck);
}
... (here are the methods "iterationUp" and so on...)
This script work perfectly. But now there is my question: how can I do the same on using gameobjects?
If I just change the type "Button" to "GameObject" I don't have the possibility to use "onClick.AddListener". I don't want a script for every single gameobject. I just want something like I posted above.
GameObject itself has no such events.
Sounds like what you are asking for is a custom UnityEvent
You can create your own component like e.g.
public class HighlightController : MonoBehaviour
{
public UnityEvent OnHighlight;
// Somewhere Invoke the event e.g.
public void SetHighlighted()
{
OnHighlight.Invoke();
}
}
where you can add listeners now either via code or via the Inspector (just like the onClicked).
So put this on your GameObjects and then e.g. have fields like
[SerializeField] private HighlightController highlight;
//etc...
private void Awake()
{
highlight.OnHighlight.AddListener(HandleHighlight);
//etc...
}
private void HandleHighlight ()
{
...
}
// Make sure to cleanup listeners when not needed anymore
private void OnDestroy()
{
highlight.OnHighlight.RemoveListener(HandleHighlight);
//etc...
}
Or yes as mentioned as simple event could do the same without the serialization:
public class HighlightController : MonoBehaviour
{
public event Action OnHighlight;
public void SetHighlighted ()
{
OnHighlight?.Invoke();
}
}
and do
private void Awake()
{
highlight.OnHighlight += HandleHighlight;
}
private void OnDestroy ()
{
highlight.OnHighlight -= HandleHighlight;
}

Control video player with button and change scene

I'm trying to create a FMV game and I would like to figure out a way to control the video player with a UI button to change scene but only after the clip has finished playing.
If the user doesn't pick anything it will automatically be a default pathway and I have managed to use loopPoinReached and it has worked.
I can figure out how to use the same loopPointReached with a button.
Here is the video player code and it will auto load the next scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadBaseCampSeq : MonoBehaviour
{
void Start()
{
GameObject camera = GameObject.Find("Main Camera");
var videoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
videoPlayer.url = "Assets/VideoAssets/intro.mp4";
videoPlayer.isLooping = false;
videoPlayer.Play();
videoPlayer.loopPointReached += EndReached;
}
void EndReached(UnityEngine.Video.VideoPlayer vp)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("baseCampSeq");
}
}
Here is the basic code that I have assigned to the UI Button On Click ()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadTruckButton : MonoBehaviour
{
public void truckSeq()
{
SceneManager.LoadScene("truckSeq");
}
}
I have been trying to reference the "EndReached" from the other script in the button but I cannot figure it out.
Any help would be awesome!
Thanks
You'll have to segregate the videoplayer functionality from the loadBaseCampSeq class into a new class. In the loadBaseCampSeq class create a videoplayer (instance) field and an abstract EndReached method:
public abstract class loadBaseCampSeq
{
public VideoPlayer videoPlayer;
public abstract void EndReached();
}
Create a button class which inherits from the loadBaseCampSeq base class. In the button class override the EndReached method that uses the videoplayer instance from it's base class:
public class Button : loadBaseCampSeq
{
public override class EndReached()
{
videoPlayer.EndReached();
}
}

Am scripting using C# in Unity but cant run code

I'm getting started with scripting in Unity and after running the attached codes. i get the error in the unity console. Any assistance for me.
Error in unity image.
This the the code i run in the VS for the unity.
enter code here
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method is Called");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update is calling");
}
}**
you have 2 Classes named CubeScript in your project. its the reason of error
Look at other script if one contains the same name of class
to avoid duplicate classes, you could add namespace too:
namespace NameOfSceneForExample
{
public class CubeScript : MonoBehaviour
{
}
}

Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}

Mimicking custom function

I have defined some custom functions in the following script and attached with my gameobject. I want to declare my function OnReticleEnter() to mimick OnPointerEnter(). Is it possible? I want this because I want to access my function OnReticleEnter() in the inspector so that I can define multiple actions like EventTrigger's Point Enter, Exit etc.
Is it possible to make the function OnReticleEnter() to behave as OnPointerEnter() programmatically?
using UnityEngine;
using System.Collections;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
public void OnReticleEnter()
{
gOBJ.SetActive(true);
}
public void OnReticleExit()
{
gOBJ.SetActive(false);
}
public void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
You need to inherit the interfaces for the pointerEnter and PointerExit events in your class to be able to implement them. Your code might look something like this :
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class MyObject : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
{
// Custom reticle events
public GameObject gOBJ;
public void OnPointerEnter(PointerEventData eventData){
gOBJ.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData){
gOBJ.SetActive(false);
}
}

Categories

Resources