Ive a question about how to change spirte images during runtime for a bunch of objects.
So i made a tiny racer 2d game, and therefore you can choose differend themes. You have this option in an integraded menu (not a seperate scene).
My question:
Can i switch the sprites easy during the runtime? Ive made prefabs for each track element - and i changed the sprites of those prefabs, but the change only gets visible, after the scene is reloaded. So i would need to avoid this.
Has someone a solution or a hint how i could do that?
Thanks in advance!
Code:
public class Background_Controller : MonoBehaviour {
public Camera mainCamera;
public Color colorNormal;
public GameObject[] Prefabs;
public Sprite[] normalSprites;
public Sprite[] tronSprites;
// Use this for initialization
void Awake () {
SwitchBackgroundFunction();
}
public void SwitchBackground(string Theme)
{
switch(Theme)
{
case "GreenHell":
PlayerPrefs.SetString("Theme", "Normal");
break;
case "NeonCity":
PlayerPrefs.SetString("Theme", "Tron");
break;
}
SwitchBackgroundFunction();
}
private void SwitchBackgroundFunction()
{
int prefabCount = Prefabs.Length;
if (PlayerPrefs.GetString("Theme") == "Normal")
{
mainCamera.backgroundColor = colorNormal;
for (int i = 0; i <= prefabCount - 1; i++)
{
Prefabs[i].GetComponent<SpriteRenderer>().sprite = normalSprites[i];
}
}
if (PlayerPrefs.GetString("Theme") == "Tron")
{
mainCamera.backgroundColor = Color.black;
for (int i = 0; i <= prefabCount - 1; i++)
{
Prefabs[i].GetComponent<SpriteRenderer>().sprite = tronSprites[i];
}
}
}
// Update is called once per frame
void Update () {
}
}
You can do something along the following lines to swap in a sprite from within your resources folder during runtime.
Sprite spr;
spr = Resources.Load<Sprite>("mysprite"); //insert name and file path to sprite within Resources folder
GetComponent<SpriteRenderer>().sprite = spr;
Related
I'm trying to develop a 3D multiplayer game with Unity. I don't know much about Photon, there are similar questions to the one I'm going to ask, but I still haven't found a solution. I will be glad if you help.
I have two scenes named "menu" and "game". In the menu scene, users make character selection after authenticating with playfab. After completing the selection, they connect to the lobby and set up a room and load the game scene. So far everything is successful. However, when the game scene is loaded, I have difficulty loading the characters selected by the users into the scene.
Here is the code file where I make the users choose their characters:
public class choose : MonoBehaviour {
private GameObject[] characterList;
private int index;
PhotonView PV;
private void Awake()
{
PV = GetComponent<PhotonView>();
}
private void Start()
{
index = PlayerPrefs.GetInt("CharacterSelected");
characterList = new GameObject[transform.childCount];
for (int i=0; i< transform.childCount; i++)
characterList[i] = transform.GetChild(i).gameObject;
foreach (GameObject go in characterList)
go.SetActive (false);
if (characterList [index])
characterList [index].SetActive (true);
}
public void ToggleLeft(){
characterList [index].SetActive (false);
index--;
if (index < 0)
index = characterList.Length - 1;
characterList [index].SetActive (true);
}
public void ToggleRight(){
characterList [index].SetActive (false);
index++;
if (index == characterList.Length)
index = 0;
characterList [index].SetActive (true);
}
public void kaydetbuton() {
PlayerPrefs.SetInt ("CharacterSelected", index);
} }
Here is the code file where I make the characters in the game move:
public class control : MonoBehaviour {
public FixedJoystick LeftJoystick;
private GameObject leftjoystick;
public FixedButton Button;
private GameObject button;
public FixedTouchField TouchField;
private GameObject touchField;
protected ThirdPersonUserControl Control;
protected float CameraAngle;
protected float CameraAngleSpeed = 0.2f;
PhotonView PV;
void Awake()
{
PV = GetComponent<PhotonView>();
}
void Start()
{
if (!PV.IsMine)
return;
Control = GetComponent<ThirdPersonUserControl>();
leftjoystick = GameObject.Find("Fixed Joystick");
if (leftjoystick != null)
{
LeftJoystick = leftjoystick.GetComponent<FixedJoystick>();
}
button = GameObject.Find("Handle (1)");
if (button != null)
{
Button = button.GetComponent<FixedButton>();
}
touchField = GameObject.Find("tfield");
if (touchField != null)
{
TouchField = touchField.GetComponent<FixedTouchField>();
}
}
void FixedUpdate() {
if (PV.IsMine)
{
Control.m_Jump = Button.Pressed;
Control.Hinput = LeftJoystick.Direction.x;
Control.Vinput = LeftJoystick.Direction.y;
CameraAngle += TouchField.TouchDist.x * CameraAngleSpeed;
Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngle, Vector3.up) * new Vector3(1, 2, 3);
Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);
}
} }
There is a game object named "karakteryükle" in the menu scene. The code file named "choose" is in this object. There are 4 characters in this game object. Each character has a code file named "control", photon view, photon transform view, animator view component. And the game object named "karakteryükle" is also available as a prefab in the Resources folder.
I am sharing the picture of the components loaded on each character
I shared a picture of the game object named "karakter yükle"
I'm trying to load "karakter yükle" when the scene named game is loaded
PhotonNetwork.Instantiate("karakteryükle", new Vector3((float)-0.43, (float)1.1, (float)-25.84), Quaternion.identity, 0, null);
Result: The "karakteryükle" is loaded onto the stage, but the same character is loaded for each player, the character chosen by each player is not loaded.
I need your opinion on this.
Each player only knows their own setting for the index, because they use the value set in PlayerPrefs.
private void Start()
{
index = PlayerPrefs.GetInt("CharacterSelected");
}
This works for our local player, no problem there. But what happens when a different player enters the scene.
The playerObject is spawned on each client.
Each client handles that playerObject locally (this is the reason IsMine exist).
Player1 executes index = PlayerPrefs.GetInt(..) on their copy of Player2.
What you can do is send a buffered RPC to set the selected character on those remote copies. We want to buffer the rpc so new players change their remote copies of everyone to the appropriate character.
myPhotonView.RPC("SetCharacterIndex", RpcTarget.OthersBuffered, index);
and the corresponding RPC method
[PunRPC]
private void SetCharacterIndex(int index)
{
// Disable other characters and enable the one at this index
}
In the end you end up with something like
void Start()
{
characterList = new GameObject[transform.childCount];
for (int i=0; i< transform.childCount; i++)
{
characterList[i] = transform.GetChild(i).gameObject;
characterList[i].SetActive(false);
}
if (isMine)
{
index = PlayerPrefs.GetInt("CharacterSelected");
// Notify all remote copies of us to change their index
//
photonView.RPC("SetCharacterIndex", RpcTarget.OthersBuffered, index);
// Set the index locally
//
SetCharacterIndex(index);
}
}
[PunRPC]
private void SetCharacterIndex(int index)
{
if (characterList [index])
characterList [index].SetActive (true);
}
Hopefully that helps clear up the reason this happens (networking can be confusing at times).
I have multiple images then push them into "Sprite[] sprites".
I create a gameobjectPrefab and add Rigidbody2d + Box Colider 2d + SpriteRenderer.
I want to Instantiate number of gameobjectPrefabs equal to the number of other sprites.
But it doesn't work, plz teach me the problem. The Sprites don't change:
public Sprite[] sprites;
public GameObject diamondPrefab;
void Start()
{
CreateDiamondsListSprites();
}
void CreateDiamondsListSprites()
{
for (int i = 0; i < sprites.Length; i++)
{
var go = diamondPrefab;
go.GetComponent<SpriteRenderer>().sprite = sprites[i];
listAllDiamondsFromSpritesList.Add(go);
// Here is sample : i add to list<gameOject> to use later
Instantiate(listAllDiamondsFromSpritesList[index],listLocationPoint[index]);
}
}
Set the sprite on the instantiated gameObject, after instantiation
void CreateDiamondsListSprites()
{
for (int i = 0; i < sprites.Length; i++)
{
var go = Instantiate(diamondPrefab,listLocationPoint[index]);
go.GetComponent<SpriteRenderer>().sprite = sprites[i];
listAllDiamondsFromSpritesList.Add(go);
}
}
So, I have the following code for switching weapons inside a placeholder and I want to extend it, to switching weapons from a button too, so I will be able to switch them on my mobile too.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WeaponSwitcher : MonoBehaviour {
public int selectedWeapon = 0;
// Use this for initialization
void Start () {
SelectWeapon();
}
// Update is called once per frame
void Update () {
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis ("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis ("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (previousSelectedWeapon != selectedWeapon)
{
SelectWeapon ();
}
}
void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive (true);
else
weapon.gameObject.SetActive (false);
i++;
}
}
}
What should I do? I'm newbie and everything I have tried didn't work :/
I have tried with an independent script just for the button to looking for which weapon is active and deactivate the other (there just two) etc but it didn't work.
public class ChangeWeapon : MonoBehaviour
{
public GameObject gun;
public GameObject shotgun;
void switchWeapons ()
{
gun = GameObject.Find("Gun");
shotgun = GameObject.Find("Shotgun");
if (gun.gameObject.activeSelf)
{
shotgun.gameObject.SetActive (true);
gun.gameObject.SetActive (false);
}
else
{
shotgun.gameObject.SetActive (false);
gun.gameObject.SetActive (true);
}
}
}
I found a really simple solution that works.
I placed two buttons on top of another, I disabled the second one and on click actions I do the followings:
Enable the second button
Disable the first button
Disable the first gun
Enable the second gun
And vise versa.
Image
I am making sniper game, I have 2 cameras, one is rendering whole environment, and second one is in scope of the gun, which has high zoom quality.
I am able to show, enemy point on screen through main camera. and it shows proper on main camera, but in scope camera(2nd camera), it not showing properly. I want to show these points exactly same as enemy is.
here is my script to show point so far. I am attaching screen shots, first one is for scope camera and does not showing proper place, and 2nd one is for main camera, showing at proper place (Red circle is pointing)
using UnityEngine;
using UnityEngine.UI;
public class identity_shower : MonoBehaviour {
public Texture tex;
public GameObject helt;
public bool target_is_high =false;
private int counter_for_high = -1;
private int counter_for_low = -1;
private int counter_value = 50;
private bool bool_for_high = false;
private bool bool_for_low = false;
private Camera scope_cam_active;
private RectTransform rectTransform;
private Vector2 uiOFFset;
// Update is called once per frame
void OnGUI()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("enemy_soldier");
if( objs.Length<=0 )
{
return;
}
if (bool_for_high)
{
Vector2 pos_to_display = Camera.main.WorldToScreenPoint (this.transform.position);
GUI.DrawTexture (new Rect(pos_to_display.x-10.0f,(Screen.height- pos_to_display.y)-40.0f,15,15),tex);
}
}
public void show_your_identity()
{
if (target_is_high) {
if (counter_for_high >= counter_value)
{
return;
}
counter_for_high = counter_for_high + 1;
if (counter_for_high >= counter_value)
{
bool_for_high = true;
}
} else if(!target_is_high)
{
if (counter_for_low >= counter_value)
{
return;
}
counter_for_low = counter_for_low + 1;
if (counter_for_low >= counter_value)
{
bool_for_low = true;
}
}
}
}
I am trying to change the Material of wall at run time. I import the model of house from Google Sketchup, which has different materials all in one object (this is shown in the inspector). Whenever I click the next button (>>), it changes the first material of the object. How do I get the references to the other elements? This is what I have so far:
public class Material_GUI : MonoBehaviour {
public Material[] mats;
public GameObject go;
private int index = 0;
// Use this for initialization
void Start () {
go.renderer.material= mats[index];
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUILayout.BeginArea(new Rect(Screen.width/2-100,Screen.height-60,200,50));
GUI.Box (new Rect(10,10,190,40),"");
GUI.Label(new Rect(62,20,100,20),"Wall Testing"+(index +1));
if(GUI.Button (new Rect(15,15,30,30),"<<")){
index--;
if(index<0){
index = mats.Length - 1;
}
go.renderer.material = mats[index];
}
if(GUI.Button (new Rect(165,15,30,30),">>")){
index++;
if(index > mats.Length -1){
index = 0;
}
go.renderer.material = mats[index];
}
GUILayout.EndArea();
}
}
If you want to change the other material of the renderer, you can use
go.renderer.materials
http://docs.unity3d.com/Documentation/ScriptReference/Renderer-materials.html?from=MeshRenderer
For example:
go.renderer.materials[0] = mats[0];
go.renderer.materials[1] = mats[1];