Convert Figma logo to code with AI

Unity-Technologies logoarfoundation-samples

Example content for Unity projects based on AR Foundation

3,312
1,217
3,312
48

Top Related Projects

ARCore SDK for Android Studio

Samples to demonstrate use of the WebXR Device API

Quick Overview

The Unity-Technologies/arfoundation-samples repository is a collection of example projects demonstrating the capabilities of AR Foundation in Unity. It showcases various augmented reality features and provides developers with practical implementations for common AR scenarios using Unity's cross-platform AR development framework.

Pros

  • Comprehensive set of AR examples covering a wide range of features
  • Cross-platform compatibility, supporting both iOS (ARKit) and Android (ARCore)
  • Regularly updated to incorporate new AR Foundation features
  • Well-documented code with clear explanations for each sample

Cons

  • Requires Unity and AR Foundation knowledge to fully understand and utilize
  • Some samples may not work on all devices due to hardware limitations
  • Limited customization options in some examples
  • May require additional setup for specific AR features or platforms

Code Examples

  1. Placing an AR object on a detected plane:
public class ARPlacement : MonoBehaviour
{
    public GameObject arObjectToPlace;
    public ARRaycastManager raycastManager;

    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                List<ARRaycastHit> hits = new List<ARRaycastHit>();
                if (raycastManager.Raycast(touch.position, hits, TrackableType.Planes))
                {
                    Pose hitPose = hits[0].pose;
                    Instantiate(arObjectToPlace, hitPose.position, hitPose.rotation);
                }
            }
        }
    }
}
  1. Detecting face features:
public class FaceDetection : MonoBehaviour
{
    public ARFaceManager faceManager;

    void OnEnable()
    {
        faceManager.facesChanged += OnFacesChanged;
    }

    void OnDisable()
    {
        faceManager.facesChanged -= OnFacesChanged;
    }

    void OnFacesChanged(ARFacesChangedEventArgs args)
    {
        foreach (ARFace face in args.added)
        {
            Debug.Log($"Face detected at position: {face.transform.position}");
        }
    }
}
  1. Capturing an AR screenshot:
public class ARScreenshot : MonoBehaviour
{
    public void CaptureScreenshot()
    {
        StartCoroutine(TakeScreenshot());
    }

    IEnumerator TakeScreenshot()
    {
        yield return new WaitForEndOfFrame();
        Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        screenshot.Apply();
        byte[] bytes = screenshot.EncodeToPNG();
        string filename = $"ARScreenshot_{System.DateTime.Now:yyyyMMddHHmmss}.png";
        System.IO.File.WriteAllBytes(Application.persistentDataPath + "/" + filename, bytes);
    }
}

Getting Started

  1. Clone the repository: git clone https://github.com/Unity-Technologies/arfoundation-samples.git
  2. Open the project in Unity (2019.3 or later recommended)
  3. Install AR Foundation and XR Plugin Management packages via Package Manager
  4. Open a sample scene from the Scenes folder
  5. Build and run on a supported AR device (iOS or Android)

Note: Ensure your Unity installation supports AR development for your target platform (iOS or Android) and that you have the necessary SDKs installed.

Competitor Comparisons

ARCore SDK for Android Studio

Pros of ARCore Android SDK

  • Native Android development with Java/Kotlin support
  • Direct access to ARCore features without middleware
  • Potentially better performance for Android-specific apps

Cons of ARCore Android SDK

  • Limited to Android platform only
  • Steeper learning curve for developers not familiar with Android development
  • Requires more low-level implementation compared to AR Foundation

Code Comparison

ARCore Android SDK (Java):

Session session = new Session(this);
Config config = new Config(session);
config.setUpdateMode(Config.UpdateMode.LATEST_CAMERA_IMAGE);
session.configure(config);

AR Foundation (C#):

ARSession session = GetComponent<ARSession>();
ARSessionOrigin sessionOrigin = GetComponent<ARSessionOrigin>();
ARPlaneManager planeManager = GetComponent<ARPlaneManager>();
planeManager.planesChanged += OnPlanesChanged;

The ARCore Android SDK code directly interacts with the AR session and configuration, while AR Foundation abstracts these operations through Unity components and events. AR Foundation provides a more Unity-centric approach, making it easier for Unity developers to implement AR features across multiple platforms.

Samples to demonstrate use of the WebXR Device API

Pros of webxr-samples

  • Web-based, allowing for easier cross-platform deployment and accessibility
  • Lightweight and doesn't require a full game engine installation
  • Utilizes standard web technologies, making it more familiar to web developers

Cons of webxr-samples

  • Limited graphics capabilities compared to Unity's robust rendering engine
  • Fewer built-in tools and features for complex AR/VR development
  • Potentially lower performance for resource-intensive applications

Code Comparison

webxr-samples:

function onXRFrame(t, frame) {
  const pose = frame.getViewerPose(xrRefSpace);
  if (pose) {
    for (const view of pose.views) {
      const viewport = xrGLLayer.getViewport(view);
      gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
      // Render scene for this view
    }
  }
}

arfoundation-samples:

void Update()
{
    if (arSession.status == ARSessionStatus.Ready)
    {
        foreach (var plane in arPlaneManager.trackables)
        {
            UpdatePlaneVisuals(plane);
        }
    }
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

AR Foundation samples

AR Foundation enables you to create multi-platform augmented reality (AR) apps with Unity. This GitHub repository contains AR Foundation samples, the official AR Foundation sample app that you can download, build to your device, and use as a starting point for your own projects.

This project demonstrates the following AR Foundation implementations:

PlatformPlug-in
AndroidGoogle ARCore XR Plug-in
iOSApple ARKit XR Plug-in
Meta QuestUnity OpenXR: Meta
Android XRUnity OpenXR: Android XR

Each AR Foundation feature is used in a minimal sample scene with example code that you can modify or copy into your project.

Which version should I use?

The main branch of this repository uses AR Foundation 6.3 and is compatible with Unity versions 6000.0 and newer. Refer to the following table for links to other branches of this repository and their corresponding Unity versions.

Unity versionAR Foundation version
Unity 6.3 beta (6000.3)6.3 main
Unity 6.2 (6000.2)6.2
Unity 6 (6000.0)6.0
2022.3 (Enterprise license only)5.2

How to use these samples

Build and run on device

You can build the AR Foundation Samples project directly to device, which can be a helpful introduction to using AR Foundation features for the first time.

To build to device, follow the steps below:

  1. Install Unity 6000.0 or newer, and clone this repository.
  2. Open the Unity project at the root of this repository.
  3. As with any other Unity project, go to the Build Profiles window, select your target platform, and build this project.

Understand the sample code

All sample scenes in this project can be found in the Assets/Scenes folder. To learn more about how each scene works, refer to the AR Foundation samples documentation, which is now published as part of the AR Foundation user manual.

Community and feedback

Refer to sections below to understand how to provide different kinds of feedback to Unity.

AR Foundation bug reports

To report a bug in AR Foundation, please file a bug. You may also submit a GitHub issue, but GitHub issues are unofficial, and we will close your GitHub issue if it does not contain an official bug ID number. The best way to ensure that your issue is addressed is to file a bug using Unity's official bug reporting process.

AR Foundation feature requests

To request a new feature in AR Foundation or related packages, use Unity's XR Roadmap. Click on the AR Foundation tab, then scroll down to Submit a New Idea.

Contributions to this repository

We are not accepting pull requests at this time. If you find an issue with the samples or would like to request a new sample, please submit a GitHub issue.