Vous êtes sur la page 1sur 6

a.

Interaction avec le Clavier

Fichier InteractionClavier.cs : Interaction avec le clavier pour déplacer l’objet,


appliquer des rotations et le détruire après n secondes.

Dans le programme ci-dessous, nous avons utilisé les flèches up down left right

, les touches (Z, F1, Space et Echap)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InteractionClavier : MonoBehaviour


{
// Start is called before the first frame update
public Vector3 monVector3;

void Start()
{
monVector3 = transform.position;
}

// Update is called once per frame


void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
this.transform.Translate(monVector3.x / 50, 0, 0);
// this.transform.Translate(10, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.transform.Translate(monVector3.x / (-50), 0, 0);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
this.transform.Translate(0, monVector3.y / (20), 0);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
this.transform.Translate(0, monVector3.y / (-20), 0);
}
else if (Input.GetKeyDown(KeyCode.Z))
{
this.transform.Translate(0, 0, monVector3.z / (20));
}
else if (Input.GetKeyDown(KeyCode.F1))
{
this.transform.Translate(0, 0, monVector3.z / (-20));
}
else if (Input.GetKeyDown(KeyCode.Space))
{
this.transform.Rotate(45, 0, 0);
//this.transform.Rotate(0, 45, 0);
//this.transform.Rotate(0, 0, 45);
//this.transform.Rotate(45, 0, 45);
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
Destroy(gameObject, 10); // Détruire l’objet après 10s
}

}
}
b. Interaction avec la souris

Fichier InteractionSouris.cs: Interaction avec la souris pour redimensionner l’objet et


déplacer la main caméra.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InteractionSouris : MonoBehaviour


{

void Start()
{

// Update is called once per frame


void Update()
{

if (Input.GetMouseButton(0))
{
this.transform.localScale += new Vector3(2, 1/2, 1);
}

if (Input.GetMouseButton(1))
{
this.transform.localScale -= new Vector3(2, 1/2, 1);
}

if (Input.GetMouseButton(2))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(ray);
Camera.main.transform.Translate(1, 0, 1);

}
}
}
c. Changement de la couleur de l’objet

Fichier ChangeMaterial.cs: Changement de la couleur RGBA de l’objet, en appuyant sur R


et/ou G et/ou B pour augmenter respectivement son intensité de rouge, vert et bleu.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeMaterial : MonoBehaviour


{
public Color altColor = Color.black;
public Renderer rend;

void Example()
{
altColor.g = 0f;
altColor.r = 0f;
altColor.b = 0f;
altColor.a = 0f;
}

void Start()
{
//Call Example to set all color values to zero.
Example();
//Get the renderer of the object so we can access the color
rend = GetComponent<Renderer>();
//Set the initial color (0f,0f,0f,0f)
rend.material.color = altColor;
}

void Update()
{
if (Input.GetKeyDown(KeyCode.G)) {
altColor.g += 0.1f; //Alter the color
rend.material.color = altColor; //Assign the changed color to the material
}
if (Input.GetKeyDown(KeyCode.R)) {
altColor.r += 0.1f; //Alter the color
rend.material.color = altColor;
}
if (Input.GetKeyDown(KeyCode.B)) {
altColor.b += 0.1f; //Alter the color
rend.material.color = altColor; //Assign the changed color to the material
}
if (Input.GetKeyDown(KeyCode.A)) {
altColor.a += 0.1f; //Alter the color
rend.material.color = altColor; //Assign the changed color to the material
}
}

}
d. Changement de la texture de l’objet

Fichier ChangeTexture.cs: Changement de la texture de l’objet

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeTexture : MonoBehaviour


{
public Texture[] Textures;
public int currentTexture;
public Renderer rend;
// Start is called before the first frame update
void Start()
{

// Update is called once per frame


void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
currentTexture++;
if (Textures.Length == 0)
return;

currentTexture %= Textures.Length;
rend.material.mainTexture = Textures[currentTexture];
}
}
}

e. Réalisation d’un bouton avec un lien

public void buttonFunction(string buttonLink)


{ Application.OpenURL(buttonLink); }

// Pour site web : http://www.isa2m.rnu.tn/

// Pour mail : mailto: Webmaster@isa2m.rnu.tn

// Pour tel : tel: + 21671603498


f. Réalisation d’un bouton virtuel

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Events;

using Vuforia;

public class VBtnFonction : MonoBehaviour

public GameObject vbBtnObj;

public GameObject obj1;

void Start()

vbBtnObj = GameObject.Find("VirtualButton");

vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonPressed(OnButt
onPressed);

vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonReleased(OnButtonReleased)
;

public void OnButtonPressed(VirtualButtonBehaviour vb)

Debug.Log("Pressed");

obj1.SetActive(false);

public void OnButtonReleased(VirtualButtonBehaviour vb)

Debug.Log("Released");

obj1.SetActive(true);

}
Autre script
using UnityEngine;

public class MenuScript : MonoBehaviour {

public void Play(string Level1)

{ SceneManager.LoadScene(Level1); }

public void Exit()

{ Application.Quit(); Debug.Log("exit"); }

public void Setting1(string SettingScene)

{ SceneManager.LoadScene(SettingScene); }

public void Setting2(string textSetting)

{ GameObject text2;

text2 = GameObject.Find(textSetting);

text2.GetComponent<Text>().text = "Setting Text here"; }

public GameObject settingWin;

public void SettingButtom( )

{ settingWin.SetActive(true); }

public void CloseSetting()

{ settingWin.SetActive(false); }

Vous aimerez peut-être aussi