#activate and deactivate object on mobile detection

1 messages · Page 1 of 1 (latest)

agile gull
#

Hi guys how can I activate a game object and deactivate another game object when it is detected that the browser is opened in mobile ?

I'm using this to detect mobile

import { isMobileDevice } from "@needle-tools/engine" if( isMobileDevice() )

zealous dawn
#

There's a handy component called DeviceFlags for that 👍

agile gull
zealous dawn
#

You can add this to an object and it turns it on or off based on your selection from the device mask

#

So, this is to drive the objects directly without you writing any code 👍

agile gull
#

for visible off i should turn off the check box ?

zealous dawn
#

yes

agile gull
#

so I'm doing this only for canvas

and I'm making a different canvas for mobile,

so i will just add this to desktop canvas and uncheck mobile? or should i uncheck for mobile for desktop canvas and for mobile canvas i should uncheck desktop ?

zealous dawn
#

Most probably you want to drive both objects

agile gull
#

okay, so will add on both the canvas

And will uncheck mobile and desktop respectively

and all other objects in the scene are activate on both mobile and desktop right ?

zealous dawn
#

yes the rest of the scene will be untouched. If you are unsure about what a component does, just open it and have a look for yourself :) Some are quite simple.

agile gull
#

do we also have something like this for scripts that enable scripts by ckecking mobile/desktop ?

covert lake
#

Not right now - but we could perhaps expose something like that in the DeviceFlags script via a UnityEvent 🤔 then you could do anything

zealous dawn
agile gull
zealous dawn
#

Sorry, could you rephrase that? I'm not sure if i follow.

agile gull
#

the above code you sent

isnt it checking if the website is opened on mobile or not ?

zealous dawn
#

Yes

agile gull
#

but how can i disable the script if i see the mobile device is detected?Sorry I'm not a Ts coder I'm using chatgpt so far.

I have two script to rotate the game object, one used mouse touch, other uses pointer (this one is for mobile)

If both scripts are enabled then the rotation on desktop is not affected but the rotation on mobile is affected and it only wants the pointer script on the object to rotated the game object smoothly, so on mouse rotation script i want that if the mobile device is detected the script disables itself

zealous dawn
#

I see, i presume both scripts are on the camera gameobject?

agile gull
#

nope the world game obbject, the worls is rotating

#

`import { Behaviour, isMobileDevice } from "@needle-tools/engine";

export class RotateObject extends Behaviour {
private isMouseDown: boolean = false;
private mouseStartX: number = 0;
private initialRotation: number = 0;
private targetRotation: number = 0;
public rotationSpeed: number = 1; // Default rotation speed is 1
public smoothness: number = 5; // Smoothness factor, adjust as needed
public startDelay: number = 3; // Delay in seconds before rotation starts
private timeElapsed: number = 0;

awake() {
this.enabled = !isMobileDevice(); // Disable the script if it's opened on a mobile device
if (!this.enabled) return;
// Rest of the awake implementation
}

start() {
window.addEventListener("mousedown", this.onMouseDown.bind(this));
window.addEventListener("mousemove", this.onMouseMove.bind(this));
window.addEventListener("mouseup", this.onMouseUp.bind(this));
}

update() {
this.timeElapsed += this.context.time.deltaTime;

if (this.isMouseDown && this.timeElapsed >= this.startDelay) {
  const mouseDeltaX = this.context.input.mousePosition.x - this.mouseStartX;
  this.targetRotation = this.initialRotation + mouseDeltaX * this.rotationSpeed;
}

// Smoothly rotate towards the target rotation
const currentRotation = this.gameObject.rotation.y;
const newRotation = this.lerp(
  currentRotation,
  this.targetRotation,
  this.smoothness * this.context.time.deltaTime
);
this.gameObject.rotation.y = newRotation;

}

onMouseDown(event: MouseEvent) {
if (event.button === 0) {
this.isMouseDown = true;
this.mouseStartX = this.context.input.mousePosition.x;
this.initialRotation = this.gameObject.rotation.y;
}
}

onMouseMove() {
if (this.isMouseDown) {
// No need for any action here
}
}

onMouseUp(event: MouseEvent) {
if (event.button === 0) {
this.isMouseDown = false;
}
}

private lerp(a: number, b: number, t: number): number {
return (1 - t) * a + t * b;
}
}`

I think this fixed the issue, i think this is disabling the mouse script after it is checking the mobile device

zealous dawn
#

For simpler code sharing, you can drop the .ts file right into discord and it will make a nice preview 👍

I see you had to wrap everything int to ` ` 😬

agile gull
#

can you please confirm if this is diabling the script on mobile device detection ?

zealous dawn
#

presumably yes 👍
this.enabled = !isMobileDevice();

you negate the result of isMobile so it works as isDesktop and thus the script is only enabled on desktop.

awake happens right when the object is created, the start should happen later and it is most probably conditioned by the enabled that we've set before. So it all should work.

But obviously you have the project in front of you, so please test it :) You can open your local build on mobile as well if you don't have a restricted network. Or you can deploy to glitch 👍

agile gull
#

yes it works perfectly right now

the only issue i find is that it has video players in it showing animated small video clips, but 99% of the time the video will not play on the mobile device

zealous dawn
#

videos can be odd, since you need to first interact / click on the page to be able to start a video. It is recommended to add a "welcome" screen that forces you to click on something.

agile gull
#

so the video is inside a small art frame, and the art appears when the gizmos gets triggered

#

and there are many like this

zealous dawn
#

So you're saying that the user interacts with the page before the video is played?

Can you deploy to glitch or show me more about the app? I'm bit unsure what is the concept here.

agile gull
#

ya wait

#

please open it on the mobile, and in the website go to the art section there you will find the video player, right mow there is only one.

also check if rotation is happening smoothly on your side on mobile or you find some glitch

zealous dawn
#

What a cute scene :)

covert lake
#

Really cute 🙂

agile gull
#

haha thanks 🙂

zealous dawn
#

Hm, so it doesn't want to play the video with these warns in the console

#

(Android - Samsung s9+ - a bit older android at this point)

agile gull
#

so any way to solve this ?

zealous dawn
#

Is the video player on a component that is disabled in the editor?

Can you potentialyl disable the play on awake and call Play from the spatial trigger?

agile gull
#

how to play from spatial trigger ?

zealous dawn
#

try this configuration (You can have the components on other objects)

agile gull
#

ok let me try

#

it is working now, kinda laggy, and also at starts the art inside the frame looks transparent, so it takes some mini second to actually start the video till then the art is transparent.

please check on your side

zealous dawn
#

You could actually put behind the screen a plane with a texture of the first frame. You would have to do that for every video.

Other variant is more code, and after first registered tap, it would play pause all videos to "wake them up" could be even worse.

I do not have experience with optimizing this, so sorry for not being able to give more advice.

zealous dawn
agile gull
agile gull
zealous dawn
agile gull
#

that was sooooooo easy 😁 🫡

#

one last question

by any chance can we change this website name in window tab? for free version ?

zealous dawn
#

Needle by default creates a vite project and that's for you to maintain and adjust.

You can start editing that by opening the workspace on the exportinfo component.

#

There should be an index.html

#

The name is usually tied to the scene name as well.

agile gull
#

or unity

#

i think u mean unity

zealous dawn
#

In unity

#

But try editing it in the html as well. there might be some default handling

agile gull
#

I'll check

thanks alot for all the help

zealous dawn
#

Let me know if it worked.
Good luck with the project 🌵

agile gull
zealous dawn
#

Now there im not sure. That might be for paid users only, since it exposes some css classes which otherwise are not.

agile gull
#

okay thanks man 🙂

covert lake
agile gull
#

thanks

agile gull
#

I have a canvas with world space, i can see the text ui in the game window but the ui doesnot apper=ar in browser

zealous dawn
#

Do you have any errors in the console during runtime?

What is the name of your font?

agile gull
#

I see that the audio doesnot start right away in browser even though its on awake, i have to first move the character for some time and then it starts to play

covert lake
#

Try clicking something in the scene. For audio to be played in the browser you need to interact with the site first (e.g. by clicking a button, touch move for example doesnt count). These are browser restrictions. That is also the reason the video doesnt play immediately (altough the video itself is allowed to play i think but it must be muted, perhaps the engine doesnt do that right!?)

agile gull
agile gull
#

how can i enable a script by sending an event from from an animation clip ?

covert lake
#

Events in animationclips are not supported - you can use signal tracks in the timeline tho to send events

#

Is that what you mean?

agile gull
#

actually i want to enable a script when a button is clicked, the script is on different object, and ofcourse the script doesnot have a checkbox, other wise i would have used bool enable in button onclick events

covert lake
#

Which script is that?

agile gull
#

the world rotation script

#

because of sound issue i added a button at start before a user can do anything, and when the button is clicked the sound starts to play, but now the world has rotation script, and so can be rotated even if the button is not clicked, so i want the rotation or rotation script to be disabled unless the button is not clicked .

covert lake
#

you can add a method like onEnable() { } in typescript so it will generate the C# OnEnable method - then you get the toggle in Unity

agile gull
#

will the toggle be controlled from onclick button events ? I dont think i will see an option for it

covert lake
#

Yes you can set enabled to on or off via a UnityEvent

agile gull
#

dont see an option to enable disable the toggle

covert lake
#

its this one

#

ah or do you want to disable this toggle?

agile gull
#

i want to keep the script off unless the button is clicked

#

once the button is clicked the script should start working

covert lake
#

Yes then turn it off in Unity and add the Unity event to set enabled to true when the button is clicked

covert lake
agile gull
#

how to add that check box ?

agile gull
#

yes that i know

#

but

covert lake
#

instead of the canvas etc it would be your RotateObject 🙂

agile gull
#

lol

covert lake
#

you already have it - no ?

agile gull
#

sorry

#

i have it already

covert lake
#

😄

agile gull
#

thanks it worked

agile gull
#

hey I have been tryting to use netlify, vut could not able to build.

  1. clicked on create, got a token
    2)Then I clicked on create website inside unity
  2. Clicked build and deploy

Still not working, am I doing something wrong ?

covert lake
#

Do you get any message in the console?

#

We saw it a few times when netlify's api didnt seem to work. Did you successfully upload? Creating a new website might take a few minutes until it is live (even if netlify's api says its ready)