有谁能让我知道如何改进我的搜索系统?
现在我只是将我在搜索框中输入的单词与我想要查找的对象的名称进行比较。但这种方式必须是完全相同的词。
我想以某种方式为每个对象提供关键字列表,如果我在搜索中点击该关键字,则会弹出我想要找到的对象。
我考虑过将关键字存储在数组中,但是如何在数组中添加单词? 我还考虑过将所有关键字都放在枚举中,这可能吗?如果是这样,怎么样?
答案 0 :(得分:1)
我设法按照我想要的方式工作。它可能不是最好的解决方案,但它适用于我现在的需求。
这仍然是一项正在进行中的工作,仍有许多事情需要改变,但目前,搜索功能仍有效,并提供关键字支持。
感谢那些快速回复的人:)
对于任何想要做同样事情的人来说,这里有一些代码:
FormulaClass:
#pragma strict
import System.Collections.Generic;
public class Formula
{
var name : String;
var formula : Texture2D;
var example : Texture2D;
//This is where i put all the words i can search for,
//to find this particular object.
var keywords : List.<String> = new List.<String>();
}
FormulaContainer:
#pragma strict
import System.Collections.Generic;
var formulas : List.<GameObject> = new List.<GameObject>();
private var myTransform : Transform;
function Start ()
{
myTransform = transform;
formulas.Add(Resources.Load("Formulas/Pythagoras", GameObject));
formulas.Add(Resources.Load("Formulas/Test2", GameObject));
formulas.Add(Resources.Load("Formulas/Test3", GameObject));
}
function Search (searchWord : String) : GameObject
{
var searchResult : GameObject;
var formulaScript : FormulaScript;
for (var formula : GameObject in formulas)
{
formulaScript = formula.GetComponent(FormulaScript);
for (var keyword : String in formulaScript.formulaClass.keywords)
{
if (keyword == searchWord)
{
searchResult = formula;
}
}
}
return searchResult;
}
FormulaScript:
#pragma strict
var formulaClass : Formula;
private var myTransform : Transform;
function Start ()
{
myTransform = transform;
formulaClass.name = myTransform.name;
formulaClass.formula = Resources.Load("Images/" + myTransform.name, Texture2D);
formulaClass.example = Resources.Load("Images/" + myTransform.name + "Example", Texture2D);
}
UI:
#pragma strict
private var myTransform : Transform;
private var currentFormula : GameObject;
private var currentInstanceFormula : GameObject;
private var size : Vector2;
private var offset : int = 25;
private var searchField : String = "";
private var container : FormulaContainer;
function Start ()
{
myTransform = transform;
container = myTransform.GetComponent(FormulaContainer);
size.x = Screen.width - offset * 2;
size.y = Screen.height - offset * 2;
}
function OnGUI ()
{
GUI.BeginGroup (new Rect (offset, offset, size.x, size.y));
GUI.Box (new Rect (0, 0, size.x, size.y), "");
searchField = GUI.TextField (Rect (25, 25, 100, 25), searchField);
if (GUI.Button (Rect (130, 25, 65, 25), "Search"))
{
currentFormula = container.Search(searchField);
if (currentInstanceFormula)
{
Destroy(currentInstanceFormula);
}
if (currentFormula)
{
Debug.LogWarning ("found " + currentFormula.transform.name + " during the search");
var newInstance : GameObject = Instantiate (currentFormula, myTransform.position, myTransform.rotation);
currentInstanceFormula = newInstance;
newInstance.transform.name = currentFormula.transform.name;
}
else
{
Debug.LogWarning ("Nothing was found during the search");
}
}
if (currentInstanceFormula)
{
var formulaScript : FormulaScript;
var name : String;
var formula : Texture2D;
var example : Texture2D;
formulaScript = currentInstanceFormula.GetComponent(FormulaScript);
name = formulaScript.formulaClass.name;
formula = formulaScript.formulaClass.formula;
example = formulaScript.formulaClass.example;
GUI.Label (new Rect (25, 75, 100, 25), name);
GUI.DrawTexture (new Rect (100, 150, 256, 64), formula);
GUI.DrawTexture (new Rect (450, 150, 512, 256), example);
}
GUI.EndGroup();
}