transform.parent = canvas.transform;
transform.SetAsLastSibling();
если я пишу в Starte prefab'а , то вылетает ошибка , The name `canvas' does not exist in the current context , нужно обозначить канвас , через public не получается , так как это префаб ,что делать?
вот код на префабе
Синтаксис:
Используется csharp
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class bannerdestroy : MonoBehaviour
{
[SerializeField] Transform banner;
[SerializeField] float speedMove = 30.0f;
[SerializeField] float offsetPosition;
void Start ()
{
StartCoroutine (MoveToDie ());
}
IEnumerator MoveToDie ()
{
Vector3 startPosition = banner.position;
Vector3 finishPosition = startPosition + Vector3.up * offsetPosition;
while (true)
{
banner.position = Vector3.MoveTowards (banner.position, finishPosition, speedMove * Time.deltaTime);
if ((banner.position - finishPosition).magnitude < 1.0f)
{
break;
}
yield return null;
}
yield return new WaitForSeconds (2.0f);
while (true)
{
banner.position = Vector3.MoveTowards (banner.position, startPosition, speedMove * Time.deltaTime);
if ((banner.position - startPosition).magnitude < 1.0f)
{
Destroy (banner.gameObject);
break;
}
yield return null;
}
}
}
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class bannerdestroy : MonoBehaviour
{
[SerializeField] Transform banner;
[SerializeField] float speedMove = 30.0f;
[SerializeField] float offsetPosition;
void Start ()
{
StartCoroutine (MoveToDie ());
}
IEnumerator MoveToDie ()
{
Vector3 startPosition = banner.position;
Vector3 finishPosition = startPosition + Vector3.up * offsetPosition;
while (true)
{
banner.position = Vector3.MoveTowards (banner.position, finishPosition, speedMove * Time.deltaTime);
if ((banner.position - finishPosition).magnitude < 1.0f)
{
break;
}
yield return null;
}
yield return new WaitForSeconds (2.0f);
while (true)
{
banner.position = Vector3.MoveTowards (banner.position, startPosition, speedMove * Time.deltaTime);
if ((banner.position - startPosition).magnitude < 1.0f)
{
Destroy (banner.gameObject);
break;
}
yield return null;
}
}
}
а вот при его создании
Синтаксис:
Используется csharp
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class UnityAds : MonoBehaviour
{
public GameObject banner, banner1;
private bool ads, net;
public Canvas canvas;
void Start ()
{
if (Advertisement.isSupported)
{
ads = true;
Advertisement.Initialize ("2578372", false);
}
else
ads = false;
}
void OnMouseUpAsButton ()
{
if (ads)
{
ShowRewardedAd ();
}
else
{
GameObject[] allGo = FindObjectsOfType<GameObject> ();
bool ins = true;
foreach (GameObject go in allGo)
{
if (go.CompareTag ("banner"))
ins = false;
}
if (ins)
{
Instantiate (banner1, new Vector2 (0, 0), Quaternion.identity);
}
}
if (Application.internetReachability == NetworkReachability.NotReachable)
{
GameObject[] allGo = FindObjectsOfType<GameObject> ();
bool ins = true;
foreach (GameObject go in allGo) //здесь 48 строчка
{
if (go.CompareTag ("banner"))
ins = false;
}
if (ins)
{
Instantiate (banner, new Vector2 (0, 0), Quaternion.identity);
}
}
}
public void ShowRewardedAd ()
{
if (Advertisement.IsReady ("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show ("rewardedVideo", options);
}
}
private void HandleShowResult (ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
if (SceneManager.GetActiveScene ().name == "shop")
{
int i = PlayerPrefs.GetInt ("allscore");
PlayerPrefs.SetInt ("allscore", i + 50);
}
break;
case ShowResult.Skipped:
break;
case ShowResult.Failed:
break;
}
}
}
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class UnityAds : MonoBehaviour
{
public GameObject banner, banner1;
private bool ads, net;
public Canvas canvas;
void Start ()
{
if (Advertisement.isSupported)
{
ads = true;
Advertisement.Initialize ("2578372", false);
}
else
ads = false;
}
void OnMouseUpAsButton ()
{
if (ads)
{
ShowRewardedAd ();
}
else
{
GameObject[] allGo = FindObjectsOfType<GameObject> ();
bool ins = true;
foreach (GameObject go in allGo)
{
if (go.CompareTag ("banner"))
ins = false;
}
if (ins)
{
Instantiate (banner1, new Vector2 (0, 0), Quaternion.identity);
}
}
if (Application.internetReachability == NetworkReachability.NotReachable)
{
GameObject[] allGo = FindObjectsOfType<GameObject> ();
bool ins = true;
foreach (GameObject go in allGo) //здесь 48 строчка
{
if (go.CompareTag ("banner"))
ins = false;
}
if (ins)
{
Instantiate (banner, new Vector2 (0, 0), Quaternion.identity);
}
}
}
public void ShowRewardedAd ()
{
if (Advertisement.IsReady ("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show ("rewardedVideo", options);
}
}
private void HandleShowResult (ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
if (SceneManager.GetActiveScene ().name == "shop")
{
int i = PlayerPrefs.GetInt ("allscore");
PlayerPrefs.SetInt ("allscore", i + 50);
}
break;
case ShowResult.Skipped:
break;
case ShowResult.Failed:
break;
}
}
}