AR Location-Based without GPS

Point Managment script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointManagment : MonoBehaviour
{
    public GameObject ARCam; //Main AR camera in our scene
    public GameObject Obj; // The object which we want to show when the user reach the point
    internal float dist; // The current distance between AR camera and object
    public float Range = 2; // The range of show the object
    private void Awake()
    {
        Obj.SetActive(false);
        if (ARCam == null)
        {
            ARCam = GameObject.Find("AR Camera");
        }
    }
    private void FixedUpdate()
    {
        if (ARCam != null)
        {
            dist = Vector3.Distance(transform.position, ARCam.transform.position);       
            if (dist < Range)
            {
                Obj.SetActive(true);
                print(gameObject.name + "Has been reached !");
            }
            if (dist > Range)
            {
                Obj.SetActive(false);
            }
        }
    }
#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        UnityEditor.Handles.color = Color.green;
        UnityEditor.Handles.DrawWireDisc(transform.position, transform.up, Range);
        UnityEditor.Handles.Label(transform.position,gameObject.name+"\nRange = "+Range);
    }
#endif
}


PointMaker

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.IO;
using System;
public class PointMaker : MonoBehaviour
{
#if UNITY_EDITOR
    internal static int counter;
    static string txt;
    internal static string[] lines;
    static GameObject gos;
   
    [MenuItem("ARLocation/Create New Point")]
    static void PointMake()
    {
        gos = GameObject.Find("Point0");
        if (gos == null)
        {
            counter = 0;
        }
        GameObject GO = Instantiate(Resources.Load("Point") as GameObject);
        GO.transform.position = new Vector3(0,0,0);
        GO.name = "Point" + counter;
        counter++;
    }
    [MenuItem("ARLocation/Read file")]
    static void ReadString()
    {
        string path = "Assets/Resources/PointsList.txt";
        StreamReader reader = new StreamReader(path);
        txt = reader.ReadToEnd().ToString();
        lines = txt.Split(new string[] { "kh" }, StringSplitOptions.None);
        for (int i = 0; i < lines.Length-1; i++)
        {
            if (lines[i].Contains("kh"))
            {
                lines[i] = lines[i].Remove(lines[i].Length - 2, 2);
            }
            print(lines[i]);
            if(!lines[i].Contains(" "))
            {
                StartProcess(lines[i]);
            }
        }
        reader.Close();
    }
    private static void StartProcess(string s)
    {
        var Obj = StringToVector3(s);
        string S = Vector3ToString(Obj);
        GameObject GO = Instantiate(Resources.Load("Point") as GameObject);
        GO.transform.position = new Vector3(Obj.PosX, Obj.PosY, Obj.PosZ);
        GO.transform.localEulerAngles = new Vector3(Obj.RotX, Obj.RotY, Obj.RotZ);
        GO.name = Obj.Name;
    }
    private static ObjectTransform StringToVector3(string s)
    {
        string[] ArrayS = s.Split('_');
        ObjectTransform objectTransform = new ObjectTransform();
        objectTransform.Name = ArrayS[0].Remove(0, 2);
        if (objectTransform.Name.Contains("N:"))
        {
            objectTransform.Name = objectTransform.Name.Replace("N:","");
        }
        if (objectTransform.Name.Contains(":"))
        {
            objectTransform.Name = objectTransform.Name.Replace(":", "");
        }
        objectTransform.PosX = float.Parse(ArrayS[1].Remove(0, 3));
        objectTransform.PosY = float.Parse(ArrayS[2].Remove(0, 3));
        objectTransform.PosZ = float.Parse(ArrayS[3].Remove(0, 3));
        objectTransform.RotX = float.Parse(ArrayS[4].Remove(0, 3));
        objectTransform.RotY = float.Parse(ArrayS[5].Remove(0, 3));
        objectTransform.RotZ = float.Parse(ArrayS[6].Remove(0, 3));
        objectTransform.ScaX = float.Parse(ArrayS[7].Remove(0, 3));
        objectTransform.ScaY = float.Parse(ArrayS[8].Remove(0, 3));
        objectTransform.ScaZ = float.Parse(ArrayS[9].Remove(0, 3));
        return objectTransform;
    }
    private static string Vector3ToString(ObjectTransform objectTransform)
    {
        string Name = "N:" + objectTransform.Name;
        string PosX = "PX:" + objectTransform.PosX;
        string PosY = "PY:" + objectTransform.PosY;
        string PosZ = "PZ:" + objectTransform.PosZ;
        string RotX = "RX:" + objectTransform.RotX;
        string RotY = "RY:" + objectTransform.RotY;
        string RotZ = "RZ:" + objectTransform.RotZ;
        string ScaX = "SX:" + objectTransform.ScaX;
        string ScaY = "SY:" + objectTransform.ScaY;
        string ScaZ = "SZ:" + objectTransform.ScaZ;
        return Name + "-" + PosX + "-" + PosY + "-" + PosZ + "-" + RotX + "-" + RotY + "-" + RotZ + "-" + ScaX + "-" + ScaY + "-" + ScaZ;
    }
    [System.Serializable]
    public struct ObjectTransform
    {
        public string Name;
        public float PosX;
        public float PosY;
        public float PosZ;
        public float RotX;
        public float RotY;
        public float RotZ;
        public float ScaX;
        public float ScaY;
        public float ScaZ;
    }
#endif
}



PointSaver

using UnityEngine;
using System.IO;
using System;
public class PointSaver : MonoBehaviour
{
    internal static int counter;
    static string txt;
    static GameObject gos;
    public GameObject ARCam;
    public void SavePoint()
    {
        gos = GameObject.Find("Point0");
        if (gos == null)
        {
            counter = 0;
        }
        GameObject GO = Instantiate(Resources.Load("Point") as GameObject);
        GO.transform.position = ARCam.transform.position;
        GO.transform.localEulerAngles = ARCam.transform.localEulerAngles;
        GO.name = "Point" + counter;
        string Pointcharactercis = "N:" + GO.name + "_PX:" + GO.transform.position.x + "_PY:" + GO.transform.position.y + "_PZ:" + GO.transform.position.z +
        "_RX:" + GO.transform.eulerAngles.x + "_RY:" + GO.transform.eulerAngles.y + "_RZ:" + GO.transform.eulerAngles.z + "_SX:" +
        GO.transform.localScale.x + "_SY:+" + GO.transform.localScale.y + "_SZ:" + GO.transform.localScale.z + "kh";
        print(Pointcharactercis);
        // It will save your file in Android/Data/app name folder/files
        string path = Application.persistentDataPath + "/PointsList.txt";
        //Write some text to the test.txt file
        StreamWriter writer = new StreamWriter(path, true);
        writer.WriteLine(Pointcharactercis);
        writer.Close();
        counter++;
    }
}



Content ©2023 Abolfazl Tanha All Rights Reserved​​​​​​​