Vivek
Vivek

Reputation: 13248

How do I hide some objects on mobile platform builds

I have created the game for both PC and mobile devices like android, ios. I have added some joystic components on the screen to help mobile users navigate the player.

The problem is that the joystic also appears on PC version as well.

How do I hide such components from being displayed on non mobile platforms of the game.

Upvotes: 3

Views: 1252

Answers (3)

Programmer
Programmer

Reputation: 125305

Detect if the platform is not a mobile device with Application.isMobilePlatform in the Awake or Start function then deactivate the Joystick GameObject. Make sure that the joystick UI stuff has a parent GameObject. That parent GameObject is what you should deactivate.

public GameObject joyStick;

void Awake()
{
    if (!Application.isMobilePlatform)
    {
        joyStick.SetActive(false);
    }
}

Again, disable GameObject, not the component. That should also disable the children objects and the scripts attached to them.

EDIT:

Thanks. I also want to the joystick script, gameobject to be excluded in non mobile platform builds. Does this also take care of it?

No. If you want it to be excluded in the scene then after creating a parent object for the joystick, make it a prefab and then delete it from the scene or the Hierarchy tab. You can then instantiate a prefab only if this is a mobile device.

public GameObject canvas;
public GameObject joyStickPrefab;

void Awake()
{
    if (Application.isMobilePlatform)
    {
        Instantiate(joyStickPrefab, canvas.transform, false);
    }
}

Note that the prefab will still be included in the build but will not be in the scene or loaded in memory unless it's a mobile device. There is really no way of excluding prefabs or assets from build. You can exclude scripts based on platform with Unity's preprocessor directives but you can't exclude assets which are the UI items from the jotystick.

Upvotes: 5

itectori
itectori

Reputation: 150

The Unity scripting API provide a static class SystemInfo.

You can easily deactivate your object if your app is running on a Desktop.

if (SystemInfo.deviceType == DeviceType.Desktop)
    gameObject.SetActive(false);

Upvotes: 0

Eliasar
Eliasar

Reputation: 1074

Answer on the Unity Q&A forums: https://answers.unity.com/questions/1426603/show-object-for-a-specific-platform-only.html

Best solution that takes the least RAM is to use Prefabs to add objects in the scene by platform condition.

Upvotes: 0

Related Questions