我这里有一些代码根据this视频对声明的单词进行加扰。下面的代码取决于我在Unity Editor中声明的单词数组。问题是我希望它是动态的,就像它将单词提取到数据库中一样。我用php编写了一个从db检索数据的代码,并在csharp上编写了一个通过WWW方法读取php的代码。我不能合并这两个过程-争夺单词和从db获取单词,请帮助我,谢谢。
this是我的Unity密码设置。如您所见,我将WordScrambe.cs附加到Core Gameobject上,并声明了两个词-“ YES”和“ YOURS”。
CharObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharObject : MonoBehaviour {
public char character;
public Text text;
public Image image;
public RectTransform rectTransform;
public int index;
[Header("Appearance")]
public Color normalColor;
public Color selectedColor;
bool isSelected= false;
public CharObject Init(char c)
{
character = c;
text.text = c.ToString ();
gameObject.SetActive (true);
return this;
}
public void Select()
{
isSelected = !isSelected;
image.color = isSelected ? selectedColor : normalColor;
if (isSelected) {
WordScramble.main.Select (this);
} else {
WordScramble.main.UnSelect();
}
}
}
WordScramble.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Word
{
public string word;
[Header("leave empty if you want randomized")]
public string desiredRandom;
public string GetString()
{
if (!string.IsNullOrEmpty (desiredRandom))
{
return desiredRandom;
}
string result = word;
while (result==word)
{
result = "";
List<char> characters = new List<char> (word.ToCharArray ());
while (characters.Count > 0)
{
int indexChar = Random.Range (0, characters.Count - 1);
//Debug.Log(characters[indexChar]);
result += characters [indexChar];
Debug.Log(word);
characters.RemoveAt (indexChar);
}
}
return result;
}// end of Getstring Method
}
public class WordScramble : MonoBehaviour {
public Word[] words;
[Header("UI REFERENCE")]
public CharObject prefab;
public Transform container;
public float space;
public float lerpSpeed=5;
List<CharObject> charobjects = new List<CharObject>();
CharObject firstSelected;
public int currentWord;
public static WordScramble main;
void Awake()
{
main = this;
}
// Use this for initialization
void Start () {
ShowScramble (currentWord);
}
// Update is called once per frame
void Update ()
{
RepositionObject ();
}
void RepositionObject()
{
if (charobjects.Count==0) {
return;
}
float center = (charobjects.Count -1) / 2;
for (int i = 0; i < charobjects.Count; i++)
{
charobjects[i].rectTransform.anchoredPosition= Vector2.Lerp(charobjects[i].rectTransform.anchoredPosition, new Vector2((i- center)* space, 0), lerpSpeed* Time.deltaTime) ;
charobjects [i].index = i;
}
}
//show a random word to the screen
public void ShowScramble()
{
ShowScramble (Random.Range (0, words.Length - 1));
}
//<summary>Show word from collection with desired index
public void ShowScramble(int index)
{
charobjects.Clear ();
foreach (Transform child in container) {
Destroy (child.gameObject);
}
//Words Finished
if (index > words.Length - 1) {
Debug.LogError ("index out of range, please enter range betwen 0-" + (words.Length - 1).ToString ());
return;
}
char[] chars = words [index].GetString ().ToCharArray ();
foreach (char c in chars)
{
CharObject clone = Instantiate (prefab.gameObject).GetComponent<CharObject> ();
clone.transform.SetParent (container);
charobjects.Add (clone.Init (c));
}
currentWord = index;
}
public void Swap(int indexA, int indexB)
{
CharObject tmpA = charobjects [indexA];
charobjects[indexA] = charobjects [indexB];
charobjects[indexB] = tmpA;
charobjects [indexA].transform.SetAsLastSibling ();
charobjects [indexB].transform.SetAsLastSibling ();
CheckWord ();
}
public void Select(CharObject charObject)
{
if (firstSelected)
{
Swap (firstSelected.index, charObject.index);
//Unselect
firstSelected.Select();
charObject.Select();
} else {
firstSelected = charObject;
}
}
public void UnSelect()
{
firstSelected = null;
}
public void CheckWord()
{
StartCoroutine (CoCheckWord());
}
IEnumerator CoCheckWord()
{
yield return new WaitForSeconds (0.5f);
string word = "";
foreach (CharObject charObject in charobjects)
{
word += charObject.character;
}
if (word == words [currentWord].word) {
currentWord++;
ShowScramble (currentWord);
}
}
}
下面是使用PHP从db检索数据并将数据传递给统一的代码。
read.php
<?php
include '../../connection.php';
$query=mysqli_query($conn, "SELECT * FROM words");
while($fetch=mysqli_fetch_array($query)){
echo $get=$fetch["words"];
echo ",";
}
?>
fetch.cs-同时,我将其附加到Unity Editor的Main Camera上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fetch : MonoBehaviour {
public string[] dbWords;
IEnumerator Start(){
WWW words=new WWW("http://localhost/bootstrap/android/v2/read.php");
yield return words;
string wordsDataString=words.text;
print(wordsDataString);
dbWords=wordsDataString.Split(',');
}
}
简而言之,我想统一制作一个争夺战游戏,它的文字取决于数据库。我有单词争夺(但是是静态的)和从数据库中检索数据但没有连接到我制作的争夺游戏的过程,这意味着我的项目还不是动态的,对于未解释的解释深感抱歉。 谢谢您的加油! :)
答案 0 :(得分:2)
欢迎您!
目前还不清楚您的问题出在哪里,但是我想您是说您没有从数据库中收到结果吗?
让我们开始将数据库逻辑移到一个单独的类中,以进行良好的练习。 同样,MonoBehaviour的Start Method的返回类型为void,而不是IENumerator。您需要一个IENumerator,可以使用StartCoroutine进行调用。
创建如下所示的单独的类
public static class NetworkManager
{
public static IEnumerator Fetch(Action<string[]> callback)
{
WWW words=new WWW("http://localhost/bootstrap/android/v2/read.php");
yield return words;
string wordsDataString=words.text;
print(wordsDataString);
var result = wordsDataString.Split(',');
callback?.Invoke(result);
}
}
我无法测试您在Fetch方法中拥有的代码,因为您是在本地使用它的,但让我们假设它现在可以使用。
请注意将回调作为参数。 这使您可以注册一个操作,该操作将在数据库调用完成后触发。
在方法的最后一行中调用它。 然后,您可以像这样调用方法:
public class SomeClass : MonoBehaviour
{
StartCoroutine(NetworkManager.Fetch( (words) =>
{
// Do something with the words!
SomeMethod(words);
});
}
协程完成后,方括号之间的所有代码都将执行。在这种情况下,将触发接受单词作为参数的“ SomeMethod”。
我希望这可以澄清并回答您的问题!